LibreOffice Regex match entire string with Regex()

Viewed 648

In J6 in my calc workbook exists the text

Greg Zuerlein kicks off 65 yards returned by Jaydon Mickens for 20 yards (tackle by Jayron Kearse)

the formula below is in another cell:

=REGEX($J6,"^.+returned by.+ for ([0-9]+)|(no gain).*$","$1")

My goal is that the formula returns the text 20. None work, and I always end up with this output:

20 yards (tackle by Jayron Kearse)

Changing the regex pattern so that it doesn't have $ or ^ doesn't change the output. Changing the $ into {0,} also does not change the output. My thought process was that the regular expression was supposed to be greedy by nature, and collect all the chars it could before matching, but that doesn't seem to be happening here. Could there be an issue with capture groups using alternation?

I've done similar searches using other programs and I'm stumped as to why this doesn't work here. Any insight would be highly appreciated.

1 Answers

You can use

^.+returned by.+ for ([0-9]+).*

See the regex demo. Details:

  • ^ - start of string/line
  • .+ - one or more chars as many as possible
  • returned by - a literal text
  • .+ - one or more chars as many as possible
  • for - a literal text
  • ([0-9]+) - Group 1 ($1): one or more digits
  • .* - zero or more chars as many as possible.
Related