Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple regular expression works on tester but not on my perl

Status
Not open for further replies.

grammarian

Programmer
Jun 23, 2006
14
US
Hi.
Here's the thing.
I have ActivePerl running on a WinXP and I'm doing this at the command
line:

perl o.pl e.txt r.txt

And this is the program o.pl:

$source=$ARGV[0];
$dest=$ARGV[1];
unless($source and $dest){
print "Source or destination file missing\n";
}

open SOURCE, "<$source";
open DEST, ">$dest";

while(<SOURCE>){
if($_ =~ m/tag(.*)/s) {
print DEST "$1\n";
}

}

close SOURCE;
close DEST;

And this is the e.txt file:

tag">word</div>
<div class="okay">

<i>o.</i> notgood,

Now I can see that here in the forum, as well as in the regex tester,
it shows up in several lines. My notepad, however, displays it on one
single line. But if I copy this now and paste it there then it shows up
in multiple lines like above.

Now when I go here:


and paste e.txt into the source textbox and tag(.*) into the pattern
textbox, and turn on Single Line and then turn off the other four, it
says my $1 is:

">word</div> <div class="okay"> <i>o.</i> notgood,

Which is correct! Then why doesn't my ActivePerl says so? Its r.txt
output is:

">word</div>

And that's obviously NOT what I want!! I have no idea why this is
happening. Do you have any idea what's happening?.

Thank you.
 
My notepad, however, displays it on one
single line.

Then it's not written in a format notepad recognizes.


here you are looping through each line of the file individually:

Code:
while(<SOURCE>){
  if($_ =~ m/tag(.*)/s) {
    print DEST "$1\n";
  }

}

which explains this output:

">word</div>

when you paste all the lines into the regexp tester they are treated as one long string and not indivdual lines.

- Kevin, perl coder unexceptional!
 
Yeah thanks, all I had to do was undef the freaking $/ variable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top