?
Solved

Regular Expresion in Perl

Posted on 2003-03-28
2
Medium Priority
?
207 Views
Last Modified: 2010-03-05
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
Comment
Question by:hemaraj123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 80 total points
ID: 8229887
$_ = "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
 
LVL 5

Expert Comment

by:burtdav
ID: 8235656
# 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question