Regex for a specific number, ignoring other numbers

I am stuck in RegEx (POSIX).

I would like to extract a specific number from my text and ignore other numbers.

For instance. I'm interested in "55", but you want to ignore 2055, 555, 0550, etc.

Disappointingly, I can not reliably say what will happen, and the beginning or end of the number, for example. it is not known if "55", "55", "55", "55", etc. I just have to assume that this is something like a number (or nothing at all).

Thank!

+4
source share
3 answers

Regular expressions have a function in which you can limit the search string to this string and only this string.

\< \ > , .

\< 55 \ > .

.

+3
^(.*[^\d])?55([^\d].*)?$

, 55 , 55 . , :

1: 55

Num: 556

, , 55 - .

+2

You can use negative appearance and negative appearance, for example:

(?<!\d)(55)(?!\d)  

Demo
Explanation:

(?<!            # Negative Look-Behind
  \d            # <digit 0-9>
)               # End of Negative Look-Behind
(               # Capturing Group (1)
  55            # "55"
)               # End of Capturing Group (1)
(?!             # Negative Look-Ahead
  \d            # <digit 0-9>
)               # End of Negative Look-Ahead
+1
source

Source: https://habr.com/ru/post/1649723/


All Articles