Skip to content

Commit 9a9effb

Browse files
committed
Add rough note about lookbehind assertions
I'm sure this example can be improved upon. This is a start nevertheless...
1 parent bd936ef commit 9a9effb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/Language/regexes.pod

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,37 @@ abracadabra
904904
abra
905905
=end code
906906
907+
=head1 Look-around assertions
908+
909+
=head2 Lookahead assertions
910+
911+
=head2 Lookbehind assertions
912+
913+
To check that a pattern appears before another pattern, one can use a
914+
lookbehind assertion via the C<after> assertion. This has the form:
915+
916+
<?after pattern>
917+
918+
Thus, to search for the string C<bar> which is immediately preceded by the
919+
string C<foo>, one could use the following regexp:
920+
921+
rx{ <?after foo> bar }
922+
923+
which one could use like so:
924+
925+
say "foobar" ~~ rx{ <?after foo> bar }; #-> bar
926+
927+
However, if you want to search for a pattern which is B<not> immediately
928+
preceded by some pattern, then you need to use a negative lookbehind
929+
assertion, this has the form:
930+
931+
<?!after pattern>
932+
933+
Hence all occurrences of C<bar> which I<do not> have C<foo> before them
934+
would be matched by
935+
936+
rx{ <?!after foo> bar }
937+
907938
=head1 Best practices and gotchas
908939
909940
Regexes and L<grammars|/language/grammars> are a whole programming paradigm

0 commit comments

Comments
 (0)