regular expression addition - lookbehind assertions and lookahead assertions #998
Labels
Enhancement
A change or improvement to an existing feature
PRG-hard
Categorized as "hard" at the Prague f2f, 2024
PRG-optional
Categorized as "optional for 4.0" at the Prague f2f, 2024
XQFO
An issue related to Functions and Operators
look-ahead assertions are i think the most useful things not found in qt regular expressions, and also look-behind.
This lets you do things like
replacing components between /..../ but not consuming the trailing /, so that
/a/b/c/d/ comes out as /../../../../
Perl uses
(?=pattern), (*pla:pattern), (*positive_lookahead:pattern)
(?!pattern), (*nla:pattern), (*negative_lookahead:pattern)
to match only if the pattern is (or is not) followed by a match to pattern,
and
(?<=pattern), \K, (*plb:pattern), (*popsitive_lookbehind:pattern)
(?<!pattern), (*nlb:pattern), (*negative_lookbehind:pattern)
for zero-width look-behind assertions.
Note, libpcre (and older Perl version) restrict lookbehind assertions to fixed length. You can write
(?<=dog|cat) food
to match " food" preceded by "dog" or "cat", but you cannot write
(?<=dogs?|cats?) barking
\C is also forbidden, as are capturing subgroups. But the facility is still very useful, and reduces the need for repeated substitutions.
I propose adding only the first form in each case, not the newer "*" forms, which are less widely supported.
The text was updated successfully, but these errors were encountered: