Home > Archive > PERL Miscellaneous > March 2006 > multi-line regular expression help
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 |
multi-line regular expression help
|
|
| Cesar Baquerizo 2006-03-31, 6:59 pm |
| Hello,
I am having a problem trying to match both of the following strings:
03/23 THU 07:09A (b12:00P b12:30P) 04:30P 8:00 0:00 0:00
as well as (embedded newline):
03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
b01:15P) 04:30P 8:00 0:00 0:00
The first string always matches, but the second never matches. I've
tried different incarnations of the following regex with no success:
(/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms)
^^
I was hoping that the above part (.*) would match a newline or anything
else in this case and return it in $&.
Any help appreciated.
Thanks
| |
| Paul Lalli 2006-03-31, 6:59 pm |
| Cesar Baquerizo wrote:
> Hello,
>
> I am having a problem trying to match both of the following strings:
>
> 03/23 THU 07:09A (b12:00P b12:30P) 04:30P 8:00 0:00 0:00
>
> as well as (embedded newline):
>
> 03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
> b01:15P) 04:30P 8:00 0:00 0:00
>
> The first string always matches, but the second never matches. I've
> tried different incarnations of the following regex with no success:
>
> (/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms)
> ^^
Odd, they both match just fine for me:
#!/usr/bin/perl
use strict;
use warnings;
#any newline in string below is the result of newsreader-wrapping
$_ = '03/23 THU 07:09A (b12:00P b12:30P) 04:30P 8:00 0:00
0:00';
if (/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms) {
print "First matched\n";
}
#newline in below string after '12:45P'
$_ = '03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
b01:15P) 04:30P 8:00 0:00 0:00';
if (/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms) {
print "Second matched\n";
}
__END__
First matched
Second matched
Perhaps you could post a short-but-complete script (as recommended by
the Posting Guidelines - have you read them yet?) which demonstrates
your error?
Paul Lalli
| |
|
| I copied and pasted your regex as well as your string and it worked with no problem:
$string = "03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
b01:15P) 04:30P 8:00 0:00 0:00";
print "$&\n" if $string =~ /^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms;
It definitely succeeded in the match and printed it fine. .* will match newlines as long as you
have /s.
Maybe if you post your code we can find the problem.
---Tome
On Fri, 31 Mar 2006 16:46:43 GMT, Cesar Baquerizo <ces@cescom.com> wrote:
>Hello,
>
>I am having a problem trying to match both of the following strings:
>
>03/23 THU 07:09A (b12:00P b12:30P) 04:30P 8:00 0:00 0:00
>
>as well as (embedded newline):
>
>03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
> b01:15P) 04:30P 8:00 0:00 0:00
>
>The first string always matches, but the second never matches. I've
>tried different incarnations of the following regex with no success:
>
>(/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms)
> ^^
>
>I was hoping that the above part (.*) would match a newline or anything
>else in this case and return it in $&.
>
>Any help appreciated.
>
>Thanks
| |
| Cesar Baquerizo 2006-03-31, 6:59 pm |
| Actually looking at both your answers made it clear what my problem was.
I incorrectly assumed that multi-line match had an effect on the
contents of $_.
As my data is on multiple lines and I was only reading in 1 line at a
time, I could never match the regex over multiple lines.
Thanks
Tome wrote:[color=darkred]
> I copied and pasted your regex as well as your string and it worked with no problem:
>
> $string = "03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
> b01:15P) 04:30P 8:00 0:00 0:00";
> print "$&\n" if $string =~ /^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms;
>
> It definitely succeeded in the match and printed it fine. .* will match newlines as long as you
> have /s.
>
> Maybe if you post your code we can find the problem.
>
> ---Tome
>
> On Fri, 31 Mar 2006 16:46:43 GMT, Cesar Baquerizo <ces@cescom.com> wrote:
>
| |
| Tad McClellan 2006-03-31, 6:59 pm |
| Cesar Baquerizo <ces@cescom.com> wrote:
> I am having a problem trying to match
I'll bet that you are having a problem...
> both of the following strings:
>
> 03/23 THU 07:09A (b12:00P b12:30P) 04:30P 8:00 0:00 0:00
>
> as well as (embedded newline):
>
> 03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
> b01:15P) 04:30P 8:00 0:00 0:00
.... getting both lines into the string that you are matching against.
Just like in the Frequently Asked Question:
perldoc -q line
I'm having trouble matching over more than one line. What's wrong?
> The first string always matches, but the second never matches.
Are you *certain* that the 2nd string contains what you think
it contains?
Did you print it out to see?
> I've
> tried different incarnations of the following regex with no success:
>
> (/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms)
It works for me...
---------------
#!/usr/bin/perl
use warnings;
use strict;
$_ = '03/27 MON 07:59A [ 11:59A 12:44P] (b12:45P
b01:15P) 04:30P 8:00 0:00 0:00';
if (/^\s*\d\d\/\d\d\s+[A-Z][A-Z][A-Z]\s+.*\d\:\d\d\s+\d\:\d\d\s*$/ms)
{ print "it matched\n" }
---------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
|
|
|