Regex For Splitting a String Using Space When Not Surrounded by Single or Double Quotes

Regex for splitting a string using space when not surrounded by single or double quotes

I don't understand why all the others are proposing such complex regular expressions or such long code. Essentially, you want to grab two kinds of things from your string: sequences of characters that aren't spaces or quotes, and sequences of characters that begin and end with a quote, with no quotes in between, for two kinds of quotes. You can easily match those things with this regular expression:

[^\s"']+|"([^"]*)"|'([^']*)'

I added the capturing groups because you don't want the quotes in the list.

This Java code builds the list, adding the capturing group if it matched to exclude the quotes, and adding the overall regex match if the capturing group didn't match (an unquoted word was matched).

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
// Add double-quoted string without the quotes
matchList.add(regexMatcher.group(1));
} else if (regexMatcher.group(2) != null) {
// Add single-quoted string without the quotes
matchList.add(regexMatcher.group(2));
} else {
// Add unquoted word
matchList.add(regexMatcher.group());
}
}

If you don't mind having the quotes in the returned list, you can use much simpler code:

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}

Regular Expression to split on spaces unless in quotes

No options required

Regex:

\w+|"[\w\s]*"

C#:

Regex regex = new Regex(@"\w+|""[\w\s]*""");

Or if you need to exclude " characters:

    Regex
.Matches(input, @"(?<match>\w+)|\""(?<match>[\w\s]*)""")
.Cast<Match>()
.Select(m => m.Groups["match"].Value)
.ToList()
.ForEach(s => Console.WriteLine(s));

java - Regex to split a string using spaces but not considering double quotes or single quotes

This regex passes your test:

" (?=(([^'\"]*['\"]){2})*[^'\"]*$)"

It's splitting on a space, but only when the space is not inside quotes, which it tests by using a look ahead to assert that there is an even number of quotes following the space.

There are some edge cases this won't work for, but if your input is "well formed" (ie quotes are balanced) this will work for you. If quotes are not balanced, it is still doable - you would need to use two look aheads - one for each quote type.


Here's some test code:

String s = "It is a \"beautiful day\"'but i' cannot \"see it\"";
String[] parts = s.split(" (?=(([^'\"]*['\"]){2})*[^'\"]*$)");
for (String part : parts)
System.out.println(part);

Output:

It
is
a
"beautiful day"'but i'
cannot
"see it"

Split R string at spaces but not when the space is between single quotes

You may use

strsplit(vec, "'[^']*'(*SKIP)(*F)|\\s+", perl=TRUE)

See the R demo and the regex demo online.

Details

  • '[^']*'(*SKIP)(*F) - ', then any 0+ chars other than ' (see [^']*) and then ', and then this matched text is discarded and the next match is searched for from the position where the current match got failed (see (*SKIP)(*F))
  • | - or
  • \s+ - 1+ whitespace chars.

Since it is a PCRE pattern, the perl=TRUE is obligatory.

Java split string with regex, anything inside Double Quotes

just a guess:

String s = "do not split this \"split this\"";
String[] split = s.split( "(?<!\".{0,255}) | (?!.*\".*)" ); // do, not, split, this, "split this"

don't split at the blank, if the blank is surrounded by quotes

split at the blank when the 255 characters to the left and all characters to the right of the blank are no quotes

Regex to split string and preserve content within double quotes

When I see a problem like this in general I immediately think to break it down into two or more simpler problems. The other thing that occurs to me is that your problem may get more complicated. It might be worth thinking about ANTLR here.

Regex for splitting a string using space when not surrounded by single or double quotes

I don't understand why all the others are proposing such complex regular expressions or such long code. Essentially, you want to grab two kinds of things from your string: sequences of characters that aren't spaces or quotes, and sequences of characters that begin and end with a quote, with no quotes in between, for two kinds of quotes. You can easily match those things with this regular expression:

[^\s"']+|"([^"]*)"|'([^']*)'

I added the capturing groups because you don't want the quotes in the list.

This Java code builds the list, adding the capturing group if it matched to exclude the quotes, and adding the overall regex match if the capturing group didn't match (an unquoted word was matched).

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
// Add double-quoted string without the quotes
matchList.add(regexMatcher.group(1));
} else if (regexMatcher.group(2) != null) {
// Add single-quoted string without the quotes
matchList.add(regexMatcher.group(2));
} else {
// Add unquoted word
matchList.add(regexMatcher.group());
}
}

If you don't mind having the quotes in the returned list, you can use much simpler code:

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}


Related Topics



Leave a reply



Submit