This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert "perlinterp: Use 'e.g' not 'i.e.' for 'for example'"
[perl5.git] / pod / perlretut.pod
index 0cbbe1f..734ca5c 100644 (file)
@@ -49,6 +49,10 @@ is harder to pronounce.  The Perl pod documentation is evenly split on
 regexp vs regex; in Perl, there is more than one way to abbreviate it.
 We'll use regexp in this tutorial.
 
+New in v5.22, L<C<use re 'strict'>|re/'strict' mode> applies stricter
+rules than otherwise when compiling regular expression patterns.  It can
+find things that, while legal, may not be what you intended.
+
 =head1 Part 1: The basics
 
 =head2 Simple word matching
@@ -292,8 +296,9 @@ class> of them.
 
 One such concept is that of a I<character class>.  A character class
 allows a set of possible characters, rather than just a single
-character, to match at a particular point in a regexp.  Character
-classes are denoted by brackets C<[...]>, with the set of characters
+character, to match at a particular point in a regexp.  You can define
+your own custom character classes.  These
+are denoted by brackets C<[...]>, with the set of characters
 to be possibly matched inside.  Here are some examples:
 
     /cat/;       # matches 'cat'
@@ -420,7 +425,7 @@ ASCII with non-ASCII characters; otherwise a Unicode "Kelvin Sign"
 would caselessly match a "k" or "K".)
 
 The C<\d\s\w\D\S\W> abbreviations can be used both inside and outside
-of character classes.  Here are some in use:
+of bracketed character classes.  Here are some in use:
 
     /\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format
     /[\d\s]/;         # matches any digit or whitespace character
@@ -436,6 +441,11 @@ of characters, it is incorrect to think of C<[^\d\w]> as C<[\D\W]>; in
 fact C<[^\d\w]> is the same as C<[^\w]>, which is the same as
 C<[\W]>. Think DeMorgan's laws.
 
+In actuality, the period and C<\d\s\w\D\S\W> abbreviations are
+themselves types of character classes, so the ones surrounded by
+brackets are just one type of character class.  When we need to make a
+distinction, we refer to them as "bracketed character classes."
+
 An anchor useful in basic regexps is the I<word anchor>
 C<\b>.  This matches a boundary between a word character and a non-word
 character C<\w\W> or C<\W\w>:
@@ -449,6 +459,11 @@ character C<\w\W> or C<\W\w>:
 Note in the last example, the end of the string is considered a word
 boundary.
 
+For natural language processing (so that, for example, apostrophes are
+included in words), use instead C<\b{wb}>
+
+    "don't" =~ / .+? \b{wb} /x;  # matches the whole string
+
 You might wonder why C<'.'> matches everything but C<"\n"> - why not
 every character? The reason is that often one is matching against
 lines and would like to ignore the newline characters.  For instance,
@@ -636,50 +651,50 @@ of what Perl does when it tries to match the regexp
 
 =over 4
 
-=item 0
+=item Z<>0
 
 Start with the first letter in the string 'a'.
 
-=item 1
+=item Z<>1
 
 Try the first alternative in the first group 'abd'.
 
-=item 2
+=item Z<>2
 
 Match 'a' followed by 'b'. So far so good.
 
-=item 3
+=item Z<>3
 
 'd' in the regexp doesn't match 'c' in the string - a dead
 end.  So backtrack two characters and pick the second alternative in
 the first group 'abc'.
 
-=item 4
+=item Z<>4
 
 Match 'a' followed by 'b' followed by 'c'.  We are on a roll
 and have satisfied the first group. Set $1 to 'abc'.
 
-=item 5
+=item Z<>5
 
 Move on to the second group and pick the first alternative
 'df'.
 
-=item 6
+=item Z<>6
 
 Match the 'd'.
 
-=item 7
+=item Z<>7
 
 'f' in the regexp doesn't match 'e' in the string, so a dead
 end.  Backtrack one character and pick the second alternative in the
 second group 'd'.
 
-=item 8
+=item Z<>8
 
 'd' matches. The second grouping is satisfied, so set $2 to
 'd'.
 
-=item 9
+=item Z<>9
 
 We are at the end of the regexp, so we are done! We have
 matched 'abcd' out of the string "abcde".
@@ -952,6 +967,12 @@ required for some reason:
     @num = split /(a|b)+/, $x;    # @num = ('12','a','34','a','5')
     @num = split /(?:a|b)+/, $x;  # @num = ('12','34','5')
 
+In Perl 5.22 and later, all groups within a regexp can be set to
+non-capturing by using the new C</n> flag:
+
+    "hello" =~ /(hi|hello)/n; # $1 is not set!
+
+See L<perlre/"n"> for more information.
 
 =head2 Matching repetitions
 
@@ -1249,35 +1270,35 @@ backtracking.  Here is a step-by-step analysis of the example
 
 =over 4
 
-=item 0
+=item Z<>0
 
 Start with the first letter in the string 't'.
 
-=item 1
+=item Z<>1
 
 The first quantifier '.*' starts out by matching the whole
 string 'the cat in the hat'.
 
-=item 2
+=item Z<>2
 
 'a' in the regexp element 'at' doesn't match the end of the
 string.  Backtrack one character.
 
-=item 3
+=item Z<>3
 
 'a' in the regexp element 'at' still doesn't match the last
 letter of the string 't', so backtrack one more character.
 
-=item 4
+=item Z<>4
 
 Now we can match the 'a' and the 't'.
 
-=item 5
+=item Z<>5
 
 Move on to the third element '.*'.  Since we are at the end of
 the string and '.*' can match 0 times, assign it the empty string.
 
-=item 6
+=item Z<>6
 
 We are done!
 
@@ -1945,10 +1966,10 @@ bit encoding, depending on the history of the string, but conceptually
 it is a sequence of characters, not bytes. See L<perlunitut> for a
 tutorial about that.
 
-Let us now discuss Unicode character classes.  Just as with Unicode
-characters, there are named Unicode character classes represented by the
+Let us now discuss Unicode character classes, most usually called
+"character properties".  These are represented by the
 C<\p{name}> escape sequence.  Closely associated is the C<\P{name}>
-character class, which is the negation of the C<\p{name}> class.  For
+property, which is the negation of the C<\p{name}> one.  For
 example, to match lower and uppercase characters,
 
     $x = "BOB";
@@ -1959,40 +1980,24 @@ example, to match lower and uppercase characters,
 
 (The "Is" is optional.)
 
-Here is the association between some Perl named classes and the
-traditional Unicode classes:
-
-  Perl class  Unicode class name or regular expression
-  name  
-
-  IsAlpha     /^[LM]/
-  IsAlnum     /^[LMN]/
-  IsASCII     $code <= 127
-  IsCntrl     /^C/
-  IsBlank     $code =~ /^(0020|0009)$/ || /^Z[^lp]/
-  IsDigit     Nd
-  IsGraph     /^([LMNPS]|Co)/
-  IsLower     Ll
-  IsPrint     /^([LMNPS]|Co|Zs)/
-  IsPunct     /^P/
-  IsSpace     /^Z/ || ($code =~ /^(0009|000A|000B|000C|000D)$/
-  IsSpacePerl /^Z/ || ($code =~ /^(0009|000A|000C|000D|0085|2028|2029)$/
-  IsUpper     /^L[ut]/
-  IsWord      /^[LMN]/ || $code eq "005F"
-  IsXDigit    $code =~ /^00(3[0-9]|[46][1-6])$/
-
-You can also use the official Unicode class names with C<\p> and
-C<\P>, like C<\p{L}> for Unicode 'letters', C<\p{Lu}> for uppercase
-letters, or C<\P{Nd}> for non-digits.  If a C<name> is just one
-letter, the braces can be dropped.  For instance, C<\pM> is the
-character class of Unicode 'marks', for example accent marks.
-For the full list see L<perlunicode>.
-
-Unicode has also been separated into various sets of characters
-which you can test with C<\p{...}> (in) and C<\P{...}> (not in).
-To test whether a character is (or is not) an element of a script
-you would use the script name, for example C<\p{Latin}>, C<\p{Greek}>,
-or C<\P{Katakana}>.
+There are many, many Unicode character properties.  For the full list
+see L<perluniprops>.  Most of them have synonyms with shorter names,
+also listed there.  Some synonyms are a single character.  For these,
+you can drop the braces.  For instance, C<\pM> is the same thing as
+C<\p{Mark}>, meaning things like accent marks.
+
+The Unicode C<\p{Script}> and C<\p{Script_Extensions}> properties are
+used to categorize every Unicode character into the language script it
+is written in.  (C<Script_Extensions> is an improved version of
+C<Script>, which is retained for backward compatibility, and so you
+should generally use C<Script_Extensions>.)
+For example,
+English, French, and a bunch of other European languages are written in
+the Latin script.  But there is also the Greek script, the Thai script,
+the Katakana script, etc.  You can test whether a character is in a
+particular script (based on C<Script_Extensions>) with, for example
+C<\p{Latin}>, C<\p{Greek}>, or C<\p{Katakana}>.  To test if it isn't in
+the Balinese script, you would use C<\P{Balinese}>.
 
 What we have described so far is the single form of the C<\p{...}> character
 classes.  There is also a compound form which you may run into.  These
@@ -2000,8 +2005,9 @@ look like C<\p{name=value}> or C<\p{name:value}> (the equals sign and colon
 can be used interchangeably).  These are more general than the single form,
 and in fact most of the single forms are just Perl-defined shortcuts for common
 compound forms.  For example, the script examples in the previous paragraph
-could be written equivalently as C<\p{Script=Latin}>, C<\p{Script:Greek}>, and
-C<\P{script=katakana}> (case is irrelevant between the C<{}> braces).  You may
+could be written equivalently as C<\p{Script_Extensions=Latin}>, C<\p{Script_Extensions:Greek}>,
+C<\p{script_extensions=katakana}>, and C<\P{script_extensions=balinese}> (case is irrelevant
+between the C<{}> braces).  You may
 never have to use the compound forms, but sometimes it is necessary, and their
 use can make your code easier to understand.
 
@@ -2011,7 +2017,7 @@ what appears to be a single character, but may be represented internally by more
 than one.  As an example, using the Unicode full names, e.g., S<C<A + COMBINING
 RING>> is a grapheme cluster with base character C<A> and combining character
 S<C<COMBINING RING>>, which translates in Danish to A with the circle atop it,
-as in the word Angstrom.
+as in the word E<Aring>ngstrom.
 
 For the full and latest information about Unicode see the latest
 Unicode standard, or the Unicode Consortium's website L<http://www.unicode.org>
@@ -2239,7 +2245,7 @@ a little background.
 
 In Perl regular expressions, most regexp elements 'eat up' a certain
 amount of string when they match.  For instance, the regexp element
-C<[abc}]> eats up one character of the string when it matches, in the
+C<[abc]> eats up one character of the string when it matches, in the
 sense that Perl moves to the next character position in the string
 after the match.  There are some elements, however, that don't eat up
 characters (advance the character position) if they match.  The examples
@@ -2293,10 +2299,6 @@ They evaluate true if the regexps do I<not> match:
     $x =~ /foo(?!baz)/;  # matches, 'baz' doesn't follow 'foo'
     $x =~ /(?<!\s)foo/;  # matches, there is no \s before 'foo'
 
-The C<\C> is unsupported in lookbehind, because the already
-treacherous definition of C<\C> would become even more so
-when going backwards.
-
 Here is an example where a string containing blank-separated words,
 numbers and single dashes is to be split into its components.
 Using C</\s+/> alone won't work, because spaces are not required between
@@ -2550,7 +2552,7 @@ running it hits the print statement before it discovers that we don't
 have a match.
 
 To take a closer look at how the engine does optimizations, see the
-section L<"Pragmas and debugging"> below.
+section L</"Pragmas and debugging"> below.
 
 More fun with C<?{}>: