I want to cut text from a string using regular expressions.
I use a regular expression:

(([0-9]+)\s+метров|([0-9]+)м) 

If they process the line "Типа 10 метров" , it cuts "10 метров" , not "10"

PS I use the group() method

  • What language is it written in? - VenZell
  • Specify a programming language. - Visman

2 answers 2

I would use positive lookahead.

For php, this option will work (it did not work with java):

 /(\d+)(?=м\b|\s+метров)/ 

Test https://regex101.com/r/sH6dD9/2

PS Both a complete match and the first group contain only numbers.

  • Thanks, it worked for me, now I won’t be littering regular expressions - NeoKat

Rewrite the request in this form:

 ([0-9]+)(\s+метров|м) 
  • Returns the same thing - NeoKat
  • @NeoKat Returns where? a complete match of the expression of course with meters, but the first capture group (first brackets) give exactly 10. regex101.com/r/kH5qW5/1 But how to get the first capture group depends on the language you write to - Mike
  • Thanks, now it works (the language in the tags indicates forgotten, if anything, Java) - NeoKat