Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-10
4
Medium Priority
?
254 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 200 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

718 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