Perl Tutorials -Part 68- Subpattern replacement

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



#an practical example of perl pattern search and replace
$_ = <<EOS;
Here is my HTML code …
If you want to know about HTML, try the  <a href=”http://www.w3c.org”>w3c</a&gt;.
Or you could check out perl at <a href=”http://www.perl.org”>Perl Mongers</a>.
EOS

#.* matches 0 or more character, ? non-greedy modifier means find the shortest match.
s/<a href=”(.*?)”>(.*?)<\/a>/$2 (LINK: $1)/g;
print;

Output:

1
2
3
Here is my HTML code ...
If you want to know about HTML, try the  w3c (LINK: http://www.w3c.org).
Or you could check out perl at Perl Mongers (LINK: http://www.perl.org).
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: