3

For an open source chat analyser in Google Sheets, I need to extract all numeric values after a substring (Example), then total them.

For example, if a cell contains Example1 another text 123 Example500 text, Example1 and Example500 should be extracted out, and their numeric values summed to 501.

This is complicated further by needing to obtain the total for a column of messages.


What I've tried already:

  • =REGEXEXTRACT(A1, "Example(\d+)"): This only extracts the first matching value, but works!
  • =SUM(SPLIT(A1, "Example")): This works for messages that only include my target string, but falls apart when other strings are included. The output could possibly be filtered to results that start with a number, but this is very messy and possibly a red herring.
  • CONCATENATEing all my cells together, then searching for numbers. This is error-prone due to additional numbers within messages.
5
  • 2
    How about the extract thing with Example(\d+) Oct 17 at 21:12
  • @bobblebubble Only gets the first one unfortunately. The regex I used is hacky and messy, yours is better, but it's trying to pull multiple out that is challenging. Looks like REGEXEXTRACT for multiple values if you have multiple matching groups.
    – Jake Lee
    Oct 17 at 21:27
  • 2
    Also try =IF(ISTEXT(A1);SUM(SPLIT(REGEXREPLACE(A1;"Example(\d+)|.";"$1 ");" "));0) Oct 17 at 23:23
  • @bobblebubble Consider adding a answer. BYROW could be useful for arraying the solution.
    – TheMaster
    Oct 18 at 14:26
  • @TheMaster I wonder how to work this BYROW in, I'm new to Google Sheets, maybe you want to edit my answer if you like :) Oct 18 at 15:55

2 Answers 2

3

Another idea is to substitute each Example(\d+) to $1 the captured digit and space |. or replace anything else with empty string (regex101 demo). Knowing that $1 is unset on the right side of the alternation. Then split on space and sum up digits (any other occurring digits have been removed). If Example is a placeholder, replace with e.g. [[:alpha:]]+ for one or more alphabetic characters.

=IF(ISTEXT(A1);SUM(SPLIT(REGEXREPLACE(A1;"Example(\d+)|.";"$1 ");" "));0)

I added IF(ISTEXT(A1);...) for only processing text in the source field (to avoid errors). Else if empty or no text it's set to 0. Just remove if the field always contains text and this is unneeded.

enter image description here

Edit from @TheMaster: As a array formula, we can use BYROW

=BYROW(A:A; LAMBDA(row; IF(ISTEXT(row); SUM(SPLIT(
 REGEXREPLACE(row;"Example(\d+)|.";"$1 ");" "));)))
0
2

try:

=LAMBDA(x, REGEXEXTRACT(A1, "(\w+)\d+")&
 SUMPRODUCT(IF(IFERROR(REGEXMATCH(x, "\w+\d+")), 
 REGEXEXTRACT(x, "\w+(\d+)"), )))(SPLIT(A1, " "))

enter image description here

update 1:

=LAMBDA(x, REGEXEXTRACT(A1, "(\D+)\d+")&
 SUMPRODUCT(IF(IFERROR(REGEXMATCH(x, "\D+\d+")), 
 REGEXEXTRACT(x, "\D+(\d+)"), )))(SPLIT(A1, " "))

enter image description here


update 2:

=INDEX(LAMBDA(xx, REGEXEXTRACT(xx, "(\D+)\d+")&
 BYROW(LAMBDA(x, IF(IFERROR(REGEXMATCH(x, "\D+\d+")), 
 REGEXEXTRACT(x, "\D+(\d+)"), ))(SPLIT(xx, " ")), LAMBDA(x, SUMPRODUCT(x))))
 (A1:INDEX(A:A, MAX((A:A<>"")*ROW(A:A)))))

if you start from A2 just change A1: to A2:

enter image description here

4
  • Damn, this was close, but only works for single digit numbers (e.g. changing first to Example99 results in Example911. That's my mistake for not stating multi-digit requirement, updating post now.
    – Jake Lee
    Oct 17 at 21:32
  • @JakeLee answer updated
    – player0
    Oct 17 at 21:41
  • That works perfectly on a per-cell basis, amazing! Only remaining challenge is totalling the operation for a range. Fumbling around with SCAN was fruitless, any suggestions for this final step?
    – Jake Lee
    Oct 17 at 21:49
  • @JakeLee answer updated
    – player0
    Oct 17 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.