Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match any nine digit number surrounded by word boundaries, but not those preceded by a period

Tags:

regex

Have a pretty specific Regex I need help building. Some restrictions: can't be multi-line, and uses the Go engine so it can't use negative lookbehinds.

Match any nine digit number surrounded by word boundaries, but not those preceded by a period.

123456789  Should match
 123456789  Should match
123456789. Should match

0.123456789  Should not match
.123456789  Should not match

https://regex101.com/r/aAd7nN/1

So far I have \b\d{9}\b, but as you'll see in the Regex101 example, it doesn't work when there's a preceding period.

Thanks!

like image 557
benjyblack Avatar asked Feb 28 '20 16:02

benjyblack


2 Answers

You may also use:

(?:^|\n|[^.])\b(\d{9})\b

and grab capture group #1 for your match.

Updated Regex Demo

like image 96
anubhava Avatar answered Oct 22 '22 11:10

anubhava


You could match what you don't want and capture in a group what you want to keep using an alternation |

\.\d{9}\b|\b(\d{9})\b

Regex demo

like image 38
The fourth bird Avatar answered Oct 22 '22 12:10

The fourth bird