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.
How do I split a string based on space but take quoted substrings as one word? \S* - followed by zero or more non-space characters.
You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.
Sometimes you might want to place quotation marks (" ") in a string of text. For example: She said, "You deserve a treat!" As an alternative, you can also use the Quote field as a constant.
Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
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.
use strict; use warnings;
my $text = "abcd efgh 'ijklm no pqrs' tuv 'xwyz 1234 9999' 'blah'";
my @out;
my @parts = split /'/, $text;
for ( my $i = 1; $i < $#parts; $i += 2 ) {
push @out, split( /\s+/, $parts[$i - 1] ), $parts[$i];
}
push @out, $parts[-1];
use Data::Dumper;
print Dumper \@out;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With