in reply to Parsing a line of text items

update

scratch it, this doesn't work. It could, but it takes too much efforts to figure it out.

Better use the Text::CSV approach


maybe

DB<45> p $_ 23 45.67 "John Marcus" Surname 23 45.67 "John Marcus" Surname DB<46> say $2 while /(?:^|("|\s+))(.*?)\1/g 45.67 John Marcus Surname 45.67 John Marcus Surname DB<47>

Here are dragons, no guaranty whatsoever.

edit

as expected, it only works if it ends with a whitespace, and I had problems using (?:$|\1) at the end.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Parsing a line of text items
by LanX (Saint) on Mar 30, 2021 at 12:20 UTC
    Just for fun:

    This seems to work if the input is surrounded by exactly one whitespace, but don't try to escape doublequotes

    • $1 is the whitespace
    • $2 the optional doublequote
    • $3 the enclosed text

    DB<89> p "'$_'" ' 23 45.67 "John Marcus" Surname 23 45.67 "John Marcus" Surname ext +ra ' DB<90> say "'$3'" while /\s*(\s)("?)(.*?)\2(?=\1)/g '23' '45.67' 'John Marcus' 'Surname' '23' '45.67' 'John Marcus' 'Surname' 'extra' DB<91>

    For testing I'd suggest to automatically create strings for random input. Like this you can cover a large set of cases.

    NB: here are still dragons.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery