-2

What is the use of
?=
in perl regex
please tell the exact meaning and give some regex example.

depsai
  • 385
  • 1
  • 13

1 Answers1

1
(?=...) 

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

Example:

.*(?=bar)

This pattern matches all the characters upto the string bar. When bar is detected then it stops matching. If a line contains more than one bar means it matches upto the last bar because .* does a greedy match.

DEMO

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229