in reply to Re^2: Question on Regular Expression
in thread Question on Regular Expression
Sometimes it's best to start simple with these things. Here's an alternate approach that seems to do what you seem to want done:
How does this match your basic requirements? What further elaborations and sophistications are needed? Do you really need named captures? Etc... (Update: You mention that you're using Perl 5.10, but both these examples, above and below, run the same for me under 5.8.9 and 5.14.4 as well as 5.10.1.)c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @test = qw(RC1XY RS); ;; for my $s (@test) { printf qq{'$s' -> }; my ($p_r, $d1, $p_mc, $p_new_mc) = $s =~ m{ ([[:upper:]]+) (?: (\d+) ([[:upper:]]) ([[:upper:]]))? }xms; dd $p_r, $d1, $p_mc, $p_new_mc; } " 'RC1XY' -> ("RC", 1, "X", "Y") 'RS' -> ("RS", undef, undef, undef)
Update: I notice you write that you want a "blank" (which I take to be an empty string) to be produced for sub-patterns that do not match. You will note that the example above yields undefined values for non-matching sub-patterns. Since the empty string and undef both have a false boolean value, I find it is usually just as easy to test and deal with undefs as with empty strings and so I usually avoid the extra effort to produce an empty string. If you really need them, here's a possible alternative approach:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @test = qw(RC1XY RS); ;; for my $s (@test) { printf qq{'$s' -> }; my ($p_r, $d1, $p_mc, $p_new_mc) = $s =~ m{ ([[:upper:]]+) (\d*) ([[:upper:]]?) ([[:upper:]]?) }xms; dd $p_r, $d1, $p_mc, $p_new_mc; } " 'RC1XY' -> ("RC", 1, "X", "Y") 'RS' -> ("RS", "", "", "")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Question on Regular Expression
by sjain (Initiate) on Dec 28, 2014 at 03:55 UTC | |
by AnomalousMonk (Archbishop) on Dec 28, 2014 at 05:57 UTC | |
by Anonymous Monk on Dec 28, 2014 at 04:41 UTC | |
by sjain (Initiate) on Dec 28, 2014 at 06:02 UTC | |
|
Re^4: Question on Regular Expression
by sjain (Initiate) on Dec 28, 2014 at 13:13 UTC | |
by AnomalousMonk (Archbishop) on Dec 28, 2014 at 17:33 UTC |