Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Answer: How can I split on a string except when inside quotes?

( #235536=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How can I split on a string except when inside quotes? - Answer contributed by Coruscate

Late entry: how about something like this:

my $string = 'first-item "second item" ' . 'third-item "fourth item"'; my %items; push @{$items{ $1 =~ /"/ ? 'quoted' : 'unquoted' }}, $1 while $string =~ /(".*?"|\S+)/g; print 'Quoted: ', join(', ', @{$items{'quoted'}}), "\n"; print 'Unquoted: ', join(', ', @{$items{'unquoted'}}), "\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2016-07-27 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (243 votes). Check out past polls.