Re: Regex Help
by Abigail-II (Bishop) on Jun 07, 2004 at 15:01 UTC
|
Are you by any chance iterating line by line over your input?
In that case, you will never match a string with an internal newline. Another potential problem are spaces you aren't noticing.
Try /ATDT\d+\s+BUSY/ (no /m needed if you aren't using ^ or $).
Abigail | [reply] [d/l] [select] |
Re: Regex Help
by Limbic~Region (Chancellor) on Jun 07, 2004 at 14:32 UTC
|
Dru,
Are you sure thats a \n newline?
#!/usr/bin/perl
use strict;
use warnings;
my $output = "Output: Got .\"ATDT123456\nBUSY";
print "matched\n" if $output =~ /ATDT\d+\nBUSY/m;
# or
print "matched\n" if $output =~ /ATDT\d+.BUSY/s;
Cheers - L~R
Update: The first question is to imply that there is more than one way to get an apparent newline. Win32 for instance uses \r\n as line endings. | [reply] [d/l] |
|
|
Thanks for the response L~R,
Yes, this is Win32. I tried:
print if /ATDT\d+.BUSY/s;
print if /ATDT\d+.*BUSY/s;
print if /ATDT\d+\r\nBUSY/m;
and I'm still not matching. Something that I didn't mention in the first post, that I didn't feel was relevant, but maybe it is. In this file, each line ends with a ^M character (I forget what these are called, form feeds?), but when I print out the entire file in a while loop, they do not appear, so I figured they did not effect the regex or do they? If they did, then this one should still work print if /ATDT\d+.*BUSY/s; right?
| [reply] [d/l] [select] |
|
|
Dru,
I am surprised that /ATDT\d+.*BUSY/s does not work. I would suggest an octal dump and since you are on Win32, you might need Perl Power Tools. It will show you what you can't see ;-)
Cheers - L~R
| [reply] |
|
|
Re: Regex Help
by Roy Johnson (Monsignor) on Jun 07, 2004 at 15:04 UTC
|
How much text do you have in your pattern space when you try the match?
/ATDT\d+\s+BUSY/ ? print : print "Did not match '$_'\n";
The PerlMonk tr/// Advocate
| [reply] [d/l] |
Re: Regex Help
by Dru (Hermit) on Jun 07, 2004 at 19:20 UTC
|
Well, I found a way to remove the control characters after each line:
perl -p -e "s/\r+//" file.txt > newfile.txt
and I thought for sure, this would work, but I still can't get it to match :-(
I tried every regex possible, but no luck, here's how I'm calling it:
while (<FILE>){
/ATDT\d+\s+BUSY/ ? print : print "Did not match '$_'\n";
#print if /ATDT\d+\s+BUSY/;
#print if /ATDT\d+.*BUSY/s;
#print;
}
L~R,
It looks like they took down the download files for Perl Power Tools.
eXile,
I tried your code, but I'm getting
The system cannot find the file specified.
I don't see where I'm suppose to specify the input file?
Abigail,
Thanks for the suggestion, but as you can see above I tried it, still not working.
Roy,
Everything gets printed back, including the lines I'm trying to match on
Did not match 'Output: Got ."ATDT1234567'
'
Did not match 'BUSY
| [reply] [d/l] [select] |
|
|
As I said, you are iterating over your input line by line. Your loop will never see a string where a newline separates "ATDT" and "BUSY". Don't loop line by line if you want to do multiline matching.
Abigail
| [reply] |
|
|
Hi,
I'm sorry, I've added a Unix-style 'echo' in my code, just to prove the concept.
Suppose you've captured the whole string in $string, you could do a rudimentary dump by:
@chars=split //, $string;
for(@chars) {
print $_ . " " . ord($_) . "\n";
}
This will print one character and its ascii decimal code per line (of course this will also output a newline as a newline, so the output isn't very pretty) | [reply] [d/l] |