Skip to content

Commit a57f595

Browse files
committed
[regexes] Anchors
at least some of them
1 parent 776ac62 commit a57f595

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

lib/Language/regexes.pod

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,81 @@ characters, followed by zero or more spaces, followed by the equals sign C<=>,
345345
followed again by optional whitespace, followed by another string of
346346
non-whitespace characters.
347347
348+
=head1 Anchors
349+
350+
The regex engine tries to find a match inside a string, by searching from left
351+
to right.
352+
353+
say so 'properly' ~~ / perl/; # True
354+
# ^^^^
355+
356+
But sometimes this is not what you want, and you want to match the whole
357+
string, or a whole line, or one or several whole words.
358+
I<Anchors> or I<assertions> can help you with that, by limiting where they
359+
match.
360+
361+
Anchors need to match successfully in order for the whole regex to match, but
362+
they do not use up characters while matching.
363+
364+
=head2 C<^>, Start of String
365+
366+
The C<^> assertion only matches at the start of the string.
367+
368+
say so 'properly' ~~ /perl/; # True
369+
say so 'properly' ~~ /^ perl/; # False
370+
say so 'perly' ~~ /^ perl/; # True
371+
say so 'perl' ~~ /^ perl/; # True
372+
373+
=head2 C<^^>, Start of Line and C<$$>, End of Line
374+
375+
The C<^^> assertion matches at the start of a logical line. That is, either at
376+
the start of the string, or after a newline character.
377+
378+
C<$$> matches only at the end of a logical line, that is, before a
379+
newline character, or at the end of the string when the last character is not
380+
a newline character.
381+
382+
(To understand the following example, it is important to know that the
383+
C<q:to/EOS/...EOS> "heredoc" syntax removes leading indention to the
384+
same level as the C<EOS> marker, so that first, second and last lines have no
385+
leading space, and the third and fourth lines have two leading spaces each).
386+
387+
my $str = q:to/EOS/;
388+
There was a young man of Japan
389+
Whose limericks never would scan.
390+
When asked why this was,
391+
He replied "It's because
392+
I always try to fit as many syllables into the last line as ever I possibly can."
393+
EOS
394+
say so $str ~~ /^^ There/; # True (start of string)
395+
say so $str ~~ /^^ limericks/; # False (not at the start of a line)
396+
say so $str ~~ /^^ I/; # True (start of the last line)
397+
say so $str ~~ /^^ When/; # False (there are blanks between
398+
# start of line and the "When")
399+
400+
say so $str ~~ / Japan $$/; # True (end of first line)
401+
say so $str ~~ / scan $$/; # False (there is a . between "scan"
402+
# and the end of line)
403+
say so $str ~~ / '."' $$/; # True (at the last line)
404+
405+
=head2 C<<< << >>> and C<<< >> >>>, left and right word boundary
406+
407+
C<<< << >>> matches a left word boundary, so positions where at the left there
408+
a non-word character (or the start of the string), and to the right there is a
409+
word character.
410+
411+
C<<< >> >>> matches a right word boundary, so positions where at the left
412+
there is a word character, and at the right there is a non-word character, or
413+
the end of the string.
414+
415+
my $str = 'The quick brown fox';
416+
say so $str ~~ /br/; # True
417+
say so $str ~~ /<< br/; # True
418+
say so $str ~~ /br >>/; # False
419+
say so $str ~~ /own/; # True
420+
say so $str ~~ /<< own/; # False
421+
say so $str ~~ /own >>/; # True
422+
348423
=head1 Grouping and Capturing
349424
350425
In regular (non-regex) Perl 6, you can use parenthesis to group things

0 commit comments

Comments
 (0)