Search code examples
regexregex-lookarounds

Explain lookaround in Regex


For putting ' before s in all Jeffs I tested these two following little different regexes and I got different results by PERL as follows :

 perl -pe "s/(?<=\bJeff)(?=s)\b/'/g" myfile.txt

What is difference between (?<=\bJeff)(?=s\b) and \b(?<=\Jeff)(?=s)\b ?

I mean putting \b inside/outside lookaround.


Solution

  • When you put \b before the lookaround, the portion of the input matched by the lookaround is not skipped over when matching \b. This means it has to be matched immediately before s, not before Jeff. So you're matching a word boundary between Jeff and s, which isn't possible since they're both part of the same word. It's effectively equivalent to

    (?<=Jeff)\b(?=s\b)