=head1 NAME
-perlfaq6 - Regexps ($Revision: 1.17 $, $Date: 1997/04/24 22:44:10 $)
+perlfaq6 - Regexps ($Revision: 1.22 $, $Date: 1998/07/16 14:01:07 $)
=head1 DESCRIPTION
# turn the line into the first word, a colon, and the
# number of characters on the rest of the line
- s/^(\w+)(.*)/ lc($1) . ":" . length($2) /ge;
+ s/^(\w+)(.*)/ lc($1) . ":" . length($2) /meg;
=item Comments Inside the Regexp
=head2 I'm having trouble matching over more than one line. What's wrong?
-Either you don't have newlines in your string, or you aren't using the
-correct modifier(s) on your pattern.
+Either you don't have more than one line in the string you're looking at
+(probably), or else you aren't using the correct modifier(s) on your
+pattern (possibly).
There are many ways to get multiline data into a string. If you want
it to happen automatically while reading input, you'll want to set $/
$/ = ''; # read in more whole paragraph, not just one line
while ( <> ) {
- while ( /\b(\w\S+)(\s+\1)+\b/gi ) {
+ while ( /\b([\w'-]+)(\s+\1)+\b/gi ) { # word starts alpha
print "Duplicate $1 at paragraph $.\n";
}
}
run up against the problem described in the question in this section
on matching balanced text.
+Here's another example of using C<..>:
+
+ while (<>) {
+ $in_header = 1 .. /^$/;
+ $in_body = /^$/ .. eof();
+ # now choose between them
+ } continue {
+ reset if eof(); # fix $.
+ }
+
=head2 I put a regular expression into $/ but it didn't work. What's wrong?
$/ must be a string, not a regular expression. Awk has to be better
this is a SUcCESS case
-=head2 How can I make C<\w> match accented characters?
+=head2 How can I make C<\w> match national character sets?
See L<perllocale>.
=head2 What's wrong with using grep or map in a void context?
-Strictly speaking, nothing. Stylistically speaking, it's not a good
-way to write maintainable code. That's because you're using these
-constructs not for their return values but rather for their
-side-effects, and side-effects can be mystifying. There's no void
-grep() that's not better written as a C<for> (well, C<foreach>,
-technically) loop.
+Both grep and map build a return list, regardless of their context.
+This means you're making Perl go to the trouble of building up a
+return list that you then just ignore. That's no way to treat a
+programming language, you insensitive scoundrel!
=head2 How can I match strings with multibyte characters?
=head1 AUTHOR AND COPYRIGHT
-Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
-All rights reserved. See L<perlfaq> for distribution information.
-
+Copyright (c) 1997, 1998 Tom Christiansen and Nathan Torkington.
+All rights reserved.
+
+When included as part of the Standard Version of Perl, or as part of
+its complete documentation whether printed or otherwise, this work
+may be distributed only under the terms of Perl's Artistic License.
+Any distribution of this file or derivatives thereof I<outside>
+of that package require that special arrangements be made with
+copyright holder.
+
+Irrespective of its distribution, all code examples in this file
+are hereby placed into the public domain. You are permitted and
+encouraged to use this code in your own programs for fun
+or for profit as you see fit. A simple comment in the code giving
+credit would be courteous but is not required.