in reply to Re^4: Question on Regular Expression
in thread Question on Regular Expression
I still don't understand if these are examples of what you are getting from a regex that "doesn't work" or examples of what you want to extract from the given strings. In any event, here's an approach that produces the given output. I don't say it's the most efficient or elegant.
(I just love playing regex Whack-A-Mole!)c:\@Work\Perl\monks>perl -wMstrict -e "my @test = qw( Sample1Repeat1A Sample2Repeat2 Sample3Repeat 4SampleRepeat 4SampleRepeat4 5SampleRepeat5D ); ;; for my $s (@test) { printf qq{'$s' -> }; if (my ($p_r, $d1, $p_mc, $p_new_mc) = $s =~ m{ \b (.+?) (?: (\d*) (?: ([[:upper:]]?) ([[:upper:]]?) )? )? \b }xms) { printf qq{'$_' } for $p_r, $d1, $p_mc, $p_new_mc; print qq{\n}; } else { print qq{no match \n}; } } " 'Sample1Repeat1A' -> 'Sample1Repeat' '1' 'A' '' 'Sample2Repeat2' -> 'Sample2Repeat' '2' '' '' 'Sample3Repeat' -> 'Sample3Repeat' '' '' '' '4SampleRepeat' -> '4SampleRepeat' '' '' '' '4SampleRepeat4' -> '4SampleRepeat' '4' '' '' '5SampleRepeat5D' -> '5SampleRepeat' '5' 'D' ''
Update: sjain: I just saw your note above about not having access to Data::Dump, so I've changed the example code to a version that uses neither that module nor Data::Dumper, the output of which doesn't look so good in this instance.
Further Update: I tried this regex with strings RS RC1XY RW12QW1X and while the latter two parse ok, RS does not, so AnonyMonk's approach below looks like a better one.
Give a man a fish: <%-(-(-(-<
|
|---|