Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: check if string is valid with special cases (updated)

by haukex (Archbishop)
on May 15, 2018 at 10:22 UTC ( [id://1214536]=note: print w/replies, xml ) Need Help??


in reply to check if string is valid with special cases

TIMTOWTDI; personally I'd write:

die "invalid format: $string" unless $string=~/\A[^,]+(?:,[^,]+){5}\z/;

Update: Note your specification is a bit unclear, you say "each one of the substrings can contain whatever symbol there is" but then later on say that the strings can't* contain commas. What about, for example, "1,2,3, ,5,6" (which the above regex will call valid)? See also Re: How to ask better questions using Test::More and sample data.

* Update 2: Sorry, I misunderstood your post, you're saying the strings can contain commas. See my reply below.

Replies are listed 'Best First'.
Re^2: check if string is valid with special cases
by ovedpo15 (Pilgrim) on May 15, 2018 at 10:30 UTC
    Im sorry, I meant that the substring can conatin commas in it. nevertheless, is it possible to include a case of space (bad case - "1,2,3, ,4,5") for the regex you wrote? I think its the regex I need.
      the substring can conatin commas in it

      Sorry, this doesn't make sense to me. If the input string is "1,2,3,4,5,6,7", then does $a get "1,2", or does $b get "2,3", and so on...

      Or is this CSV, as swl correctly pointed out? Then you should use Text::CSV.

      If not, then as I said, please provide lots of examples of valid input with the expected output, as well as lots of examples of invalid input.

        "a ,b,c,d,e" is okay (space with substring) but one space or more (without any symbols) isn't valid. For example: "a, ,c,d,e" or "a,b,c, d,e" are not valid.
        Ok I understand, forget about commas in the substrings.
        your regex is good and its what I need except it doesn't handle an empty space case: "a,b,c, d,e" (bad case).
        how can I add it to the regex?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214536]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-20 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found