What does ?= mean in a regular expression?

Regex

Regex Problem Overview


May I know what ?= means in a regular expression? For example, what is its significance in this expression:

(?=.*\d).

Regex Solutions


Solution 1 - Regex

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured.

Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

Solution 2 - Regex

(?=pattern) is a zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $&.

Solution 3 - Regex

The below expression will find the last number set in a filename before its extension (excluding dot (.)).

'\d+(?=\.\w+$)'

file4.txt will match 4.

file123.txt will match 123.

demo.3.js will match 3 and so on.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontheranemanView Question on Stackoverflow
Solution 1 - RegexcletusView Answer on Stackoverflow
Solution 2 - RegexMichael FoukarakisView Answer on Stackoverflow
Solution 3 - RegexKishor UView Answer on Stackoverflow