Solved

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

Posted on 2008-06-10
4
241 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

749 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