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


In reply to Re^5: Question on Regular Expression by AnomalousMonk
in thread Question on Regular Expression by sjain

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.