Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Regular Expresion in Perl

Hai,

Iam having text as follows:

"This is Sample Text. <i>This is Sample Text. <i>This is Sample</i> Text. This is Sample Text. This is <i>Sample Text</i>. This is Sample Text. This is Sample Text. <i>This is Sample Text.</i> This is Sample Text. This is Sample Text. This is Sample Text. This is Sample Text.</i> This is Sample Text. This is Sample Text. This is Sample Text."

I want to strip the text inbetween <i> and </i> tags. But I want the exact match of the <i> tag. If I match with Reg. Exp. as:

/<i>.*?<\/i>/s

then I will get the result as:

"<i>This is Sample Text. <i>This is Sample</i>"

but this is not the case. I want a single regular expresion to match the following text:

"<i>This is Sample Text. <i>This is Sample</i> Text. This is Sample Text. This is <i>Sample Text</i>. This is Sample Text. This is Sample Text. <i>This is Sample Text.</i> This is Sample Text. This is Sample Text. This is Sample Text. This is Sample Text.</i>"

Is there any expresion to match this.

thanks

hemaraj123
0
hemaraj123
Asked:
hemaraj123
1 Solution
 
ozoCommented:
$_ = "This is Sample Text. <i>This is Sample Text. <i>This is Sample</i> Text. This is Sample Text. This is <i>Sample Text</i>. This is Sample Text. This is Sample Text. <i>This is Sample Text.</i> This is Sample Text. This is Sample Text. This is Sample Text. This is Sample Text.</i> This is Sample Text. This is Sample Text. This is Sample Text.";
($re=$_)=~s/((<i>)|(<\/i>)|.)/${[')','']}[!$3]\Q$1\E${['(','']}[!$2]/gs;
$re = join'|',map quotemeta,eval{/$re/};
die $! if $!;
print /(<i>(?:$re)<\/i>)/;
0
 
burtdavCommented:
# Alternatively, you could try a serial search.
for ($i = 0; $i < length($bigstring) - 3; $i++) {
    if (lc(substr($bigstring, $i, 3)) eq "<i>")
        push @a, $i;
    elsif (lc(substr($bigstring, $i, 4)) eq "</i>") {
        $start = pop @a;
        print substr($bigstring, $start, ($i - $start + 4));
    }
}
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now