I think the problem is simply that the [^\1]+ is greedy. Changing it to [^\1]+? seems to work:

$foo = qq{^snafu^|^foobar^\n}; $foo =~ m/\A(\W) # \A instead of ^ and match first non-word [^\1]+? # Match everything that isn't in \1 \1(\W) # Match non-word following the 2nd \1 /xms; $text_qual = $1; $field_sep = $2; print "[$text_qual]\n"; print "[$field_sep]\n";
Output:
[^] [|]

Update: Nope I think I'm at least partially wrong too. Greedy or not, if \1 is ^, then [^\1]+ should stop at the first ^.

Changing [^\1]+ to the hardcoded [^^]+ shows that greedyness doesn't matter. So it does appear to have something to do with \1 inside character classes.

Update 2: I think .*? instead of [^\1]+ might work, but it contains the dreaded dot-star. I assume the real solution uses some form of lookahead, but I've never been good with those.


In reply to Re: Regular Expresssion TroubleShoot Help plz by Crackers2
in thread Regular Expresssion TroubleShoot Help plz by MajingaZ

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.