Solved

Simple perl regular expression help: Searching between a new line?

Posted on 2008-06-10
4
249 Views
Last Modified: 2011-09-20
I am designing an HTML optimizer program, and I want to remove all extra unneeded <b> and </b>'s.

When $html =
<b>foo</b> <b>bar</b>
<b>test</b><b>cool</b>
<b>foo2</b>
<b>bar2</b>

And my code snippet is:
while ($html =~ s!</b>(|\s+)<b>! !i)
{
           print $html;
}

My output looks like:
<b>foo bar</b>
<b>test cool</b>

... but why was "<b>foo2</b>
<b>bar2</b>" ignored? Doesn't the /s+ mean that it searches for all blank spaces, new lines, etc?
0
Comment
Question by:sprockston
[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
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 21756851
It should have looked like
<b>foo bar</b>
<b>test</b><b>cool</b>
<b>foo2</b>
<b>bar2</b>
(first time)
<b>foo bar test</b><b>cool</b>
<b>foo2</b>
<b>bar2</b>
(second time)
<b>foo bar test cool</b>
<b>foo2</b>
<b>bar2</b>
(third time)
<b>foo bar test cool foo2</b>
<b>bar2</b>
(fourth time)
<b>foo bar test cool foo2 bar2</b>
(fifth time)
0
 

Author Comment

by:sprockston
ID: 21757054
Well, my complete code was:

open (FILE, "testfile.html") || die "Could not open the file <$!>";
while (my $html = <FILE>)
{
# Remove all blank (empty) lines from the html file.
$html =~ s/(^|\n)[\n\s]*/$1/g;

      while ($html =~ s!</b>(|\s+)<b>! !i)
      {
             print $html;
      }
}
close (FILE);
0
 

Author Comment

by:sprockston
ID: 21757069
...is the way I am reading in the file to a variable the reason why it doesn't work?
0
 
LVL 84

Expert Comment

by:ozo
ID: 21757184
Assuming you left $/ with it's default value of 1
$html will not contain
<b>foo</b> <b>bar</b>
<b>test</b><b>cool</b>
<b>foo2</b>
<b>bar2</b>
when you read the first line of <FILE>, $html will contain
<b>foo</b> <b>bar</b>
then after
$html =~ s!</b>(|\s+)<b>! !i
it will become
<b>foo bar</b>
when you read the second line of <FILE>
$html will contain
<b>test</b><b>cool</b>
then after
$html =~ s!</b>(|\s+)<b>! !i
it will become
<b>test cool</b>
when you read the third line of  <FILE>
$html will contain
<b>foo2</b>
and
$html =~ s!</b>(|\s+)<b>! !i
will fail
and on the last line of
<FILE>
$html will contain
<b>bar2</b>
and again
$html =~ s!</b>(|\s+)<b>! !i
will fail
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

627 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