in reply to Re^3: Question on Regular Expression
in thread Question on Regular Expression

Here are more example strings :

1. Sample1Repeat1A -> ("Sample1Repeat", "1", "A", "")

2, Sample2Repeat2 -> ("Sample2Repeat", "2", "", "")

3. Sample3Repeat -> ("Sample3Repeat", "", "", "")

4. 4SampleRepeat -> ("4SampleRepeat", "", "", "")

5. 4SampleRepeat4 -> ("4SampleRepeat", "4", "", "")

6. 5SampleRepeat5D -> ("4SampleRepeat", "5", "D", "")

I hope these samples help giving more information.

Replies are listed 'Best First'.
Re^5: Question on Regular Expression
by AnomalousMonk (Archbishop) on Dec 28, 2014 at 17:33 UTC

    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.

    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' ''
    (I just love playing regex Whack-A-Mole!)

    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:   <%-(-(-(-<