-1

Appreciating regex but still beginning.

I tried many workarounds but can't figure how to solve my problem.

String A : 4 x 120glgt

String B : 120glgt

I'd like the proper regex to return 120 as the number after "x". But sometimes there won't be "x". So, be it [A] or [B] looking for one unique approach.

I tried :

  1. to start the search from the END
  2. Start right after the "x"

I clearly have some syntax issues and didn't quite get the logic of (?=)

(?=[^x])(?=[0-9]+)

So looking forward to learn with your help

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125

1 Answers1

1

As you tagged pcre, you could optionally match the leading digits followed by x and use \K to clear the match buffer to only match the digits after it.

^(?:\d+\h*x\h*)?\K\d+

The pattern matches:

  • ^ Start of string
  • (?:\d+\h*x\h*)? Optionally match 1+ digits followed by x between optional spaces
  • \K Forget what is matched so far
  • \d+ Match 1+ digits

See a regex demo.

If you want to use a lookahead variant, you might use

\d+(?=[^\r\n\dx]*$)

This pattern matches:

  • \d+ Match 1+ digits
  • (?= Positive lookahead, assert what is to the right is
    • [^\r\n\dx]*$ Match optional repetitions of any char except a digit, x or a newline
  • ) Close the lookahead

See another regex demo.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • @fourth thx a lot. GSheets REGEXEXTRACT invokes regex syntax error. If someone knows how to transcribe to GSheets regex syntax... – David ABITEBOUL Mar 21 '22 at 15:24
  • @DavidABITEBOUL Try it with REGEXREPLACE, match `^(?:\d+\h*x\h*)?(\d+).+` and replace with capture group 1 like `"$1"` See https://regex101.com/r/6ApGxb/1 – The fourth bird Mar 21 '22 at 16:05
  • @fourth I still can't get it. Sorry for my dumbiness. I share a little GSheet with the [example](https://docs.google.com/spreadsheets/d/12v5BgH9KwLeCPNEymbelvHmc4uMNDeGnnBgy2f2umww/edit?usp=sharing) with, of course, edition rights. – David ABITEBOUL Mar 21 '22 at 16:21
  • @DavidABITEBOUL It could be like this `=REGEXREPLACE(A2,"^(?:\d+\s*x\s*)?(\d+).+", "$1")` See https://imgur.com/a/OJDijQ0 Or use `=REGEXEXTRACT(A2,"^(?:\d+\s*x\s*)?(\d+)")` – The fourth bird Mar 21 '22 at 16:31