Perl Tutorials -Part 56- Character sets

Posted on June 3, 2010. Filed under: Perl Tutorials |


#perl pattern matching example, character set

$_ = 'A fox, a box, and an ox.';
#[a-z] stands for any character from a to z
@matches = m/[a-z]ox/g;
foreach (@matches)
{
    print "$_\n";
}

Output:

1
2
fox
box
#perl pattern matching example, special characters

$_ = 'A fox, a box, and an -ox.';
#[a\-z] stands for a character a,-, or z
@matches = m/[a\-z]ox/g;
foreach (@matches)
{
    print "$_\n";
}

print "\n\n";
#[^f] stands for any characters except f
@matches = m/[^f]ox/g;
foreach (@matches)
{
    print "$_\n";
}

Output:

1
2
3
4
5
-ox

box
-ox
Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: