perl
view release on metacpan or search on metacpan
pod/perlrequick.pod view on Meta::CPAN
$x = "I batted 4 for 4";
$x =~ s/4/four/g; # $x contains "I batted four for four"
The non-destructive modifier C<s///r> causes the result of the substitution
to be returned instead of modifying C<$_> (or whatever variable the
substitute was bound to with C<=~>):
$x = "I like dogs.";
$y = $x =~ s/dogs/cats/r;
print "$x $y\n"; # prints "I like dogs. I like cats."
$x = "Cats are great.";
print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~
s/Frogs/Hedgehogs/r, "\n";
# prints "Hedgehogs are great."
@foo = map { s/[a-z]/X/r } qw(a b c 1 2 3);
# @foo is now qw(X X X 1 2 3)
The evaluation modifier C<s///e> wraps an C<eval{...}> around the
replacement string and the evaluated result is substituted for the
matched substring. Some examples:
# reverse all the words in a string
$x = "the cat in the hat";
$x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"
# convert percentage to decimal
$x = "A 39% hit rate";
$x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"
The last example shows that C<s///> can use other delimiters, such as
C<s!!!> and C<s{}{}>, and even C<s{}//>. If single quotes are used
C<s'''>, then the regex and replacement are treated as single-quoted
strings.
=head2 The split operator
C<split /regex/, string> splits C<string> into a list of substrings
and returns that list. The regex determines the character sequence
that C<string> is split with respect to. For example, to split a
string into words, use
$x = "Calvin and Hobbes";
@word = split /\s+/, $x; # $word[0] = 'Calvin'
# $word[1] = 'and'
# $word[2] = 'Hobbes'
To extract a comma-delimited list of numbers, use
$x = "1.618,2.718, 3.142";
@const = split /,\s*/, $x; # $const[0] = '1.618'
# $const[1] = '2.718'
# $const[2] = '3.142'
If the empty regex C<//> is used, the string is split into individual
characters. If the regex has groupings, then the list produced contains
the matched substrings from the groupings as well:
$x = "/usr/bin";
@parts = split m!(/)!, $x; # $parts[0] = ''
# $parts[1] = '/'
# $parts[2] = 'usr'
# $parts[3] = '/'
# $parts[4] = 'bin'
Since the first character of $x matched the regex, C<split> prepended
an empty initial element to the list.
=head2 C<use re 'strict'>
New in v5.22, this applies stricter rules than otherwise when compiling
regular expression patterns. It can find things that, while legal, may
not be what you intended.
See L<'strict' in re|re/'strict' mode>.
=head1 BUGS
None.
=head1 SEE ALSO
This is just a quick start guide. For a more in-depth tutorial on
regexes, see L<perlretut> and for the reference page, see L<perlre>.
=head1 AUTHOR AND COPYRIGHT
Copyright (c) 2000 Mark Kvale
All rights reserved.
This document may be distributed under the same terms as Perl itself.
=head2 Acknowledgments
The author would like to thank Mark-Jason Dominus, Tom Christiansen,
Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpful
comments.
=cut
( run in 0.725 second using v1.01-cache-2.11-cpan-71847e10f99 )