Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: validating a quoted string

by Corion (Patriarch)
on Jan 13, 2016 at 13:44 UTC ( [id://1152677]=note: print w/replies, xml ) Need Help??


in reply to validating a quoted string

How does your regular expression fail?

A first step would be to explain the regular expression and tell us in English words what ([.]?) is supposed to do.

Ideally, describe in English what the complete regular expression should do.

My approach to solving the problem would be to take all parts of a string between double quotes and check each part that it doesn't contain a space. Alternatively, look at all "words" that are delimited with whitespace, and check that they either contain no double quotes or start with a double quote and end with a double quote.

Update: I misread part of the problem:

my "dog shepherd" <-- valid

So that would imply that simply checking for an even number of double quotes is the simplest part.

Replies are listed 'Best First'.
Re^2: validating a quoted string
by hellosarathy (Novice) on Jan 13, 2016 at 13:49 UTC
    Sorry, My bad, typo, it should be
    $in =~ /(["]?)[^" ]+\1/
Re^2: validating a quoted string
by hellosarathy (Novice) on Jan 13, 2016 at 13:51 UTC
    my query:
    $in =~ /(["]?)[^" ]+\1/
    does'nt work as expected. I can have a string separated by spaces. Sometimes I can also have two or more strings within quotes to be considered as a single string (just like command line arguments we pass to a script).

      A problem with your current regular expression is that [^" ]+ does not allow spaces.

      This fails for example for the following string, which I think should be valid:

      Corion says "hello sarathy"

      If you want to stay with your approach of matching the tokens (words or quoted parts), I suggest reading perlre, especially on alternation.

      For such an approach, I would restate the problem as Match every token that starts with a letter and consists only of letters, or starts with a double-quote and consists of non-double-quotes..

      I think that you want to parse the string the same way that a shell parses a command-line. The result is a list of sub-strings. It probably is a good idea to do the validation (use previous suggestions) before attempting the parse. I cannot think of a good way to do the parsing. I hope I have put other monks on the right track.

      UPDATE

      You can parse the strings with Text::CSV

      #!/usr/bin/perl use warnings; use strict; use Text::CSV; my @strings = ( '"my" "dog"', '"my" "dog shepherd"', 'my dog', 'my "dog shepherd"', '"my "dog"', 'my "dog shepherd', ); my $csv = Text::CSV->new ( {sep_char => ' '} ) or die "Cannot use CSV: ".Text::CSV->error_diag (); foreach my $string (@strings){ open my $fh, '<', \$string or die "Cannot open string"; if (((my $temp = $string) =~ tr/"//) % 2){ warn "invalid string"; next; } my $row = $csv->getline($fh); if (!defined $row) { warn "getline failed"; next; } close $fh; $" = ' | '; print "@$row\n"; }
      Bill

Log In?
Username:
Password:

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

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

    No recent polls found