Front page | perl.perl5.porters |
Postings from April 2000
Re: [Long] A candidate for perlretut.pod
Thread Previous
|
Thread Next
From:
Peter Haworth
Date:
April 7, 2000 06:17
Subject:
Re: [Long] A candidate for perlretut.pod
Message ID:
ML-3.3.955113502.5758.pmh@edison.ioppublishing.com
Mark Kvale wrote:
> I've written
> a Perl regular expression tutorial and am submitting it as a candidate
> for perlretut.pod.
I've only skimmed this, so I might have missed other problems.
> =head2 Using character classes
> /\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format
> /[\d\s]/; # matches any digit or whitespace character
> /\w\W\w/; # matches a word char, followed by a
> # non-word char, followed by a word char
> /..rt/; # matches any two chars, followed by 'rt'
> /end\./; # matches 'end.'
You haven't introduced '.' as a metacharacter yet. Also, the distinction here
should probably be between /./ and /[.]/, rather than /./ and /\./
> "" =~ //; # matches automatically
No it doesn't. You explain what it actually does later on, though.
> If C<//m> is being used, the start
> and the end of string can still be matched with the anchors C<\A> and
> C<\Z>:
You mean \z. \Z matches before a newline at the end, too
> =head2 Matching this or that
> "cats and dogs" =~ /dog|cat|bird/; # matches "dog"
matches "cat"
> "cats and dogs" =~ /cat|dog|bird/; # matches "cat"
> "birddog" =~ /cat|dog|bird/; # matches "dog"
matches "bird"
> "catbird" =~ /cat|dog|bird/; # matches "cat"
> "jaybird" =~ /cat|dog|bird/; # matches "bird"
> "cats" =~ /c|ca|cat|cats/; # matches "c"
> "cats" =~ /cats|cat|ca|c/; # matches "cats"
> "cab" =~ /a|b|c/ # matches "a"
matches "c"
> # /a|b|c/ == /[abc]/
You manage to explain this OK in the backtracking section, so this must have
just been a slip up
> =head2 Using regular expressions in Perl
> $metric = 1; # use metric units
> ...
> $x = <FILE>; # read in measurement
> $x =~ /^([+-]?\d+)\s*/; # get magnitude
Shouldn't this have a /g modifier?
> $weight = $1;
> if ($metric) { # error checking
> print "Units error!" unless $x =~ /\Gkg\./g;
> }
> else {
> print "Units error!" unless $x =~ /\Glbs\./g;
> }
> $x =~ /\G\s+(widget|sprocket)/; # continue processing
> Search and replace is accomplished with the
> C<s///> operator. The general form is
> C<s/regexp/replacement/modifiers>, with everything we know about
> regexps and modifiers applying in this case as well. The
> C<replacement> is a Perl expression that replaces in the string
> whatever is matched with the C<regexp>.
C<replacement> is generally a double quoted string. It's only an expression
with the /e modifier.
> If you prefer 'regex' over 'regexp' in this tutorial, you could use
> the following program to replace it:
>
> % cat > simple_replace
> #!/usr/bin/perl
> $regexp = shift;
> $replacement = shift;
> while (<>) {
> print if s/$regexp/$replacement/go;
> }
That sohuld be
s/$regexp/$replacement/go;
print;
unless you only want the changed lines.
Unfortunately, I ran out of time before I got much further
--
Peter Haworth pmh@edison.ioppublishing.com
I would not, could not SAVE ON PHONE, I would not, could not BUY YOUR LOAN,
I would not, could not MAKE MONEY FAST, I would not, could not SEND NO CA$H,
I would not, could not SEE YOUR SITE, I would not, could not EAT VEG-E-MITE,
I do *not* *like* GREEN CARDS AND SPAM!
Thread Previous
|
Thread Next