How about using variables to turn confusion to clarity? Better yet, go to the library and borrow Perl Best practices to see why '\A' is better than \$'. spreading out your regex and adding comments, helps, too.

my $ham = "spam\tspam\tspam\tyam\tclam"; my $word = qr{[^\t]+}; my $sep = qr{\t}; my $capture = qr{($word)}; my (@jam ) = ($ham =~ m{\A # enforce beginning of stri +ng $word $sep # skip first word and separ +ator $word $sep # and the second $capture $sep # capture next two words $capture $sep # skip the separators $word # skip a word \z # and then it's the end of +the string }xms); print join("\n", '**', @jam, '**', '');

Using the debugger helps, too ... Along the way I noticed your string has a double tab '\t\t', but you only ever accept single tabs; You specify end of string '$', when there's still another word to go.

But you're doing too much work. You could split() on '\t' and select only the components you want. If it's the 3rd % 4th ...

my @jam = ( spit "\t", $ham )[2,3];

If you do need to use a regex, do you need to check whether there is a word after your capture? Do you need to enforce there is nothing after that last word? Simplify your regex for greater happiness.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


In reply to Re: How to capture quantified repeats? by TomDLux
in thread How to capture quantified repeats? by whatever

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.