Home > Blockchain >  How do I match a number not followed by a letter except for a certain substring
How do I match a number not followed by a letter except for a certain substring

Time:12-01

I am trying to match a number not followed by a letter except if followed by the substring See
I am not even sure if this is possible but I've tried the following which did not work.

enter image description here

My question is: How do I match a number (number 7 in the example above) which is not followed by a letter except when followed by the substring See
There should also be a match if not a letter. e.g. dot, comma, dash

CodePudding user response:

The 2 separate lookahead assertions are from the current position.

You can assert See to the right, but using (?![A-Za-z]) will prevent to match it as it starts with S. If you remove that part, you still have an optional lookahead, which will cause more digits to match.

If you make the lookahead not optional, you will match a digit followed by a space, but you only want the digit so you can add the space to the lookahead.

You could update the pattern to:

\b\d (?= See\b)

See a regex demo.

To prevent partial matches, you can surround the pattern with word boundaries \b

Or you can capture 1 (or more) digits in a group followed by matching See without using any lookarounds.

\b(\d ) See\b

See a regex demo

Edit

If you also want to allow matching a digit followed by a dot, comma or dash, there should be optional spaces before See to allow another character directly after the digit.

To prevent partial matches for the digit, you can assert a whitespace boundary to the left, and allow either See or any of the allowed characters using a character class to the right.

(?<!\S)\d (?!\.\d)(?=\s*(?:See\b|[.,-]))

Regex demo

  • Related