Home: Perl Programming Help: Regular Expressions:
Problem with regex



FiMko
New User

Sep 25, 2009, 12:11 PM


Views: 5730
Problem with regex

Hi all!

I have a problem with regex given below. Here is the target text:

Quote
Something text
TT-AAA-MMMM-1929 Something text
123 Something text
Something text 456 Something text


I just want to catch "TT-AAA-MMMM-1929" making a start from "123" or "456".
So, this regex works in my regex constructor (I don't know what type of engine it is based on) but the perl script finds nothing...

Code
@regexps = ["123", "456"]; 
for ($i = 0; $i < 2; $i++) {
$txt =~ m/TT-AAA-MMMM-\d{4}(?=(.+\r\n)*.+regexps[$i])/g;
print($&);
}


Could anybody help me with this regex?
Thanks in advance.


FiMko
New User

Sep 25, 2009, 12:54 PM


Views: 5726
Re: [FiMko] Problem with regex

Seems a problem with line breaks \r?\n works...


shawnhcorey
Enthusiast


Sep 26, 2009, 6:10 AM


Views: 5686
Re: [FiMko] Problem with regex

Try this:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $txt = <<EOD;
Something text
TT-AAA-MMMM-1929 Something text
123 Something text
Something text 456 Something text
EOD

my @regexps = ("123", "456");
for my $regexp ( @regexps ){
$txt =~ m/TT-AAA-MMMM-\d{4}(?=(.+\r?\n)*.+$regexp)/s;
print "$regexp: $&\n";
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".