Skip to content

regular expression addition - lookbehind assertions and lookahead assertions #998

Description

@liamquin

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

  replace( ., '
     / ( [^/]+ ) (*positive_lookahead: /)
    ', '...', 'x')

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementA change or improvement to an existing featurePR PendingA PR has been raised to resolve this issuePRG-hardCategorized as "hard" at the Prague f2f, 2024PRG-optionalCategorized as "optional for 4.0" at the Prague f2f, 2024XQFOAn issue related to Functions and Operators

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions