Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I agree with others that this looks like a Text::CSV problem (and also that your various problem statements are rather vague).

However, in a general regex parsing (and regexes are often not the best approach to parsing) situation, my approach tends to be something like

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = 'a,b,c,d,e,f'; ;; my $sym = qr{ [^,] }xms; my $sep = qr{ , }xms; ;; $s =~ m{ \A $sym (?: $sep $sym){5} \z }xms or die qq{bad string: '$s' +}; ;; my ($u, $v, $w, $x, $y, $z) = $s =~ m{ $sym }xmsg; dd $u, $v, $w, $x, $y, $z; " ("a", "b", "c", "d", "e", "f")
Once you know a string is valid, it's often quite easy to strip out sub-strings of interest. Oh, you say the separator pattern should include possible spaces? Then
    my $sep = qr{ \s* , \s* }xms;
Oh, the "symbol" may be more than a single character and must also exclude spaces? Then
    my $sym = qr{ [^,\s]+ }xms;
And so on. Separately defining $sym and $sep makes them easy to change and makes any change propagate throughout the code as necessary (DRY).

And yes, test all this stuff! (Test::More and friends.)

Update: Here's another approach to regex (again, maybe not the best option) parsing. It combines validation and extraction into a single regex, but the regex is significantly more complex and probably a bit slower. It also needs Perl version 5.10+ for the  \K operator.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $s = 'a,b, CC ,d , e,fgh '; ;; my $sym = qr{ [^,\s]+ }xms; my $sep = qr{ \s* , \s* }xms; ;; my $n_syms = my ($u, $v, $w, $x, $y, $z) = $s =~ m{ (?: \G (?! \A) $sep | \A \s*) \K $sym (?= (?: $sep $sym)* \s* \z) }xmsg; ;; $n_syms == 6 or die qq{bad string: '$s'}; dd $u, $v, $w, $x, $y, $z; " ("a", "b", "CC", "d", "e", "fgh")
(Testing, testing...)

Give a man a fish:  <%-{-{-{-<

In reply to Re: check if string is valid with special cases (updated) by AnomalousMonk
in thread check if string is valid with special cases by ovedpo15

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2019-12-11 02:26 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found