Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now