Perl Tutorials -Part 62- Subpatterns: 1

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


#perl pattern matching example, reference sub-pattern outside regex $n
$_ = 'The room was full of boxes and bins.';
/([a-z]oxes) and (bins)\./;
#the whole matched pattern
print $&;
#the matched pattern by 1st ()
print "\n";
print $1;
print "\n";
#the pattern matched by 2nd ()
print $2;

Link: http://codepad.org/soh5EAj7 [ raw code | output | fork ] Save this paste Delete this paste
Perl, pasted 1 second ago:
1
2
3
4
5
6
7
8
9
10
11
#perl pattern matching example, reference sub-pattern outside regex $n
$_ = 'The room was full of boxes and bins.';
/([a-z]oxes) and (bins)\./;
#the whole matched pattern
print $&;
#the matched pattern by 1st ()
print "\n";
print $1;
print "\n";
#the pattern matched by 2nd ()
print $2;

Output:

1
2
3
boxes and bins.
boxes
bins
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: