Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

786 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