regex before

Documentation for regex before, assembled from the following types:

language documentation Regexes

From Regexes

(Regexes) regex before

To check that a pattern appears before another pattern, use a lookahead assertion via the before assertion. This has the form:

<?before pattern>

Thus, to search for the string foo which is immediately followed by the string bar, use the following regexp:

rx{ foo <?before bar> }

For example:

say "foobar" ~~ rx{ foo <?before bar> };  # OUTPUT: «foo␤» 

However, if you want to search for a pattern which is not immediately followed by some pattern, then you need to use a negative lookahead assertion, this has the form:

<!before pattern>

Hence, all occurrences of foo which is not before bar would match with

say "foobar" ~~ rx{ foo <!before bar> }   # OUTPUT: «Nil␤» 

Lookahead assertions can be used also with other patterns, like characters ranges, interpolated variables, subscripts and so on. In such cases it does suffice to use a ? (or a ! for the negate form). For instance, the following lines all produce the very same result:

say 'abcdefg' ~~ rx{ abc <?before def> };        # OUTPUT: 「abc」 
say 'abcdefg' ~~ rx{ abc <?[ d..f ]> };          # OUTPUT: 「abc」 
my @ending_letters = <d e f>;
say 'abcdefg' ~~ rx{ abc <?@ending_letters> };   # OUTPUT: 「abc」