in reply to Re: validating a quoted string
in thread validating a quoted string

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).

Replies are listed 'Best First'.
Re^3: validating a quoted string
by Corion (Patriarch) on Jan 13, 2016 at 13:57 UTC

    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..

Re^3: validating a quoted string
by BillKSmith (Monsignor) on Jan 13, 2016 at 14:41 UTC
    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