2

I have a text:

Wheels – F/R_ Schwalbe TABLE TOP/Schwalbe Black Jack 26x2.2

And regex to parse wheels size from that string:

/.*Wheels.*(\d*)x/

But it does not work. Besides, when i'm removing asterisk from regex, i'm getting number 6 as group match.

Src
  • 5,252
  • 5
  • 28
  • 56

2 Answers2

4

You need to make your .* before the digits lazy instead of greedy:

/.*Wheels.*?(\d*)x/

The .* will greedily consume everything up to the x, leaving nothing for the following \d*. Since * can validly match zero characters, an empty match for \d* is not an incorrect result.

By adding a ? to make a lazy .*? expression, it will try match as few characters as possible, allowing the following \d* to match all the numbers before the x.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
2

You need to make your regex non-greedy because .* will consume your digits and \d* mean zero or no match

.*Wheels.*?(\d*)x 

.*? mean match as many characters as few time as possible to stop .* to consume your digits

Follow this Demo for example


Alternately you can make it more efficient if there are no digits after Wheel and your desired values with following regex

.*Wheels[^\d]*(\d*)x

where [^\d]* mean matches anything except digits

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68