Home > Blockchain >  Regex: match only last 2 digits and ignore whitespaces at the end of line
Regex: match only last 2 digits and ignore whitespaces at the end of line

Time:03-15

Example:

32-12•
32-12•••
32-12-52••
32-12-53-12

(let's say Bullet Point "•" is Whitespaces)

What I have tried is

/(?<=^.*)\d{2}(?= *)$/gm

but it seem like it does match only last 2 digits that whitespaces doesn't concat like this

32-12•
32-12•••
32-12-52••
32-12-53-12

(let's say bold strings are where regex matched)

but what I want is last 2 digits ignore whitespaces like this

32-12
32-12•••
32-12-52••
32-12-53-12

CodePudding user response:

You can use

\d{2}(?= *$)

See the regex demo. To match any whitespaces, replace the literal space with as \s shorthand character class: \d{2}(?=\s*$).

Details:

  • \d{2} - two digits
  • (?= *$) - a positive lookahead that requires zero or more chars and the end of string position to appear immediately to the right of the current location.
  • Related