Home > Archive > PERL Beginners > November 2006 > simple regex works on tester but not on my perl
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
simple regex works on tester but not on my perl
|
|
| cyborg 2006-11-19, 6:57 pm |
| 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:
http://regexlib.com/RETester.aspx
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. Please help me, I learn on my own but I can't move forward
unless my Perl works the same as everyone else's. Thank you.
| |
| cyborg 2006-11-19, 9:56 pm |
| Some guy at the other forum found the solution to my problem.
It was that $/ had to be set to undef:
$/ = undef;
And that was all.
cyborg wrote:
> 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:
>
> http://regexlib.com/RETester.aspx
>
> 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. Please help me, I learn on my own but I can't move forward
> unless my Perl works the same as everyone else's. Thank you.
|
|
|
|
|