Dru has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm trying to match on the following text:
Output: Got ."ATDT123456 BUSY
Here's the regex that I thought would work:
print if /ATDT\d+\nBUSY/m;
But it does not. I can match each one individually, but not together.

Thanks,
Dru

Replies are listed 'Best First'.
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

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.

      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?
        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

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
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
      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

      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)