-2

What is difference between The following syntaxs in regular expression?

Please give an example.

(?=.*\d)

and

.*(?=\d)
Ehsan
  • 11,709
  • 3
  • 20
  • 40

3 Answers3

4

The first one is just an assertion, a positive look-ahead saying "there must be zero or more characters followed by a digit." If you match it against a string containing at least one digit, it will tell you whether the assertion is true, but the matched text will just be an empty string.

The second one searches for a match, with an assertion (a positive-lookahead) after the match saying "there must be a digit." The matched text will be the characters before the last digit in the string (including any previous digits, because .* is greedy, so it'll consume digits up until the last one, because the last one is required by the assertion).

Note the difference in the match object results:

var str = "foo42";
test("rex1", /(?=.*\d)/, str);
test("rex2", /.*(?=\d)/, str);

function test(label, rex, str) {
  console.log(label, "test result:", rex.test(str));
  console.log(label, "match object:", rex.exec(str));
}

Output (for those who can't run snippets):

rex1 test result: true
rex1 match object: [
  ""
]
rex2 test result: true
rex2 match object: [
  "foo4"
]

Notice how the match result in the second case was foo4 (from the string foo42), but blank in the first case.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
1

(?=...) is a positive lookahead. Both of these expressions will match "any text followed by a number". The difference, though, is that (?=...) doesn't "eat" ("capture") any characters as it matches. For practical purposes, if this is the only thing your regex contains, they'll match the same stuff. However, .*(?=\d) would be a more correct expression, unless there's more to it than what you put in the question.

Where it really matters is when you're using capturing groups or where you're using the content of the matched text after running the regular expression:

If you want to capture all text before the number, but not the number itself, and use it after, you could do this:

(.*?(?=\d))

The ? makes the match non-greedy, so it will only match up to the first number. All text leading up to the number will be in the match result as the first group.

Will
  • 21,498
  • 11
  • 84
  • 98
0

Please find the difference below In detail

.* means matches any character (except newline)
(?=\d) means Positive Lookahead - Assert that the regex below can be matched \d match a digit [0-9]

(?=.*\d)

CapturingGroup  

MatchOnlyIfFollowedBy
Sequence: match all of the followings in order
Repeat
AnyCharacterExcept\n
zero or more times
Digit

.*(?=\d)

Sequence: match all of the followings in order
Repeat
AnyCharacterExcept\n
zero or more times
CapturingGroup
MatchOnlyIfFollowedBy
Digit

Naga Sai A
  • 9,949
  • 1
  • 15
  • 34
  • Please find few regular expression parser/decoder just for reference http://www.myezapp.com/apps/dev/regexp/show.ws https://regex101.com/ – Naga Sai A Jun 19 '16 at 06:55