Pacific-Design.com

    
Home Index

1. Perl5

2. Regex

+ Call Sub

+ Classes

+ Emails

+ Non-Greedy

Perl5 / Regex /

Perl regex matching tag

#!/usr/bin/perl
#-------------------------------------------------------------------------------------#
use strict;
use warnings;
#-------------------------------------------------------------------------------------#

my $text = "<b><i>first</i> second <i>third</i> fourth</b> <b><i>fifth</i></b>";

$text =~  m{ <b><i>(.*?)</i></b> }sx;
print "Match All: " . $1 . "\n";

$text =~ m{ <b><i>(  (?: (?!</[ib]>). )*  ) </i></b> }sx;
print "Match 5th: " . $1 . "\n";

#--------------------------------------------------------------------------------------#
# Output:
# Match All: first</i> second <i>third</i> fourth</b> <b><i>fifth
# Match 5th: fifth
#--------------------------------------------------------------------------------------#