How can I split a string by whitespace unless inside of a single quoted string?
perl, regex, split
Solution
Use Text::ParseWords:
#!/usr/bin/perl
use strict; use warnings;
use Text::ParseWords;
my @words = parse_line('\s+', 0, "abcd efgh 'ijklm no pqrs' tuv");
use Data::Dumper;
print Dumper \@words;
Output:
C:\Temp> ff
$VAR1 = [
'abcd',
'efgh',
'ijklm no pqrs',
'tuv'
];
You can look at the source code for `Text::ParseWords::parse_line` to see the pattern used.
Problem
I'm seeking a solution to splitting a string which contains text in the following format: ``` "abcd efgh 'ijklm no pqrs' tuv" ``` which will produce the following results: ``` ['abcd', 'efgh', 'ijklm no pqrs', 'tuv'] ``` In other words, it splits by whitespace unless inside of a single quoted string. I think it could be done with .NET regexps using "Lookaround" operators, particularly balancing operators. I'm not so sure about Perl.