Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the regex (\d{3})(?=\d) mean? [duplicate]

I am new to regex, and I am trying to break down the regex so I can understand it better:

 /(\d{3})(?=\d)/ 

I understand that (\d{3}) is capturing 3 digits, but unsure what the second portion is trying to capture.

What does ?= mean?

like image 315
DHuang Avatar asked Dec 25 '22 14:12

DHuang


2 Answers

(?=\d) is a positive lookahead it means match & capture 3 digits that are followed by a digit.

So something like this will happen:

1234 => capture 123
123a => no match
like image 77
anubhava Avatar answered Jan 04 '23 19:01

anubhava


(?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text

/(\d{3})(?=\d)/ - Here (\d{3}) is capturing 3 digits, followed by a digit,but last digit not to be captured in that group.

Look here, here and here

Hope this will help!

like image 34
Arup Rakshit Avatar answered Jan 04 '23 18:01

Arup Rakshit