Regular expression to split by space but ignore double-quotes string

Asked
Active3 hr before
Viewed126 times

7 Answers

stringsplitexpressionregular
90%

Meta Stack Overflow ,I know /[\s]+/' splits a string by spaces. I am trying to expand this to ignore any spaces within double quotes. So I want Hello World to be split, but not "Hello World", Stack Overflow Public questions & answers , Stack Overflow help chat

You can use the PCRE verbs (*SKIP)(*FAIL) to tell the regex to skip specific parts of an expression. So:

".*?"( * SKIP)( * FAIL) | \s +
88%

I need help splitting a string in javascript by space (" "), ignoring space inside quotes expression.,Use a CSV parser like OpenCSV to take care of things like commas in quoted elements, values that span multiple lines etc. automatically. You can use the library to serialize your text back as CSV as well.,Turns out, to fix your original expression, you just need to add a + on the group:,I would expect my string to be split to 2:

I have this string:

var str = 'Time:"Last 7 Days" Time:"Last 30 Days"';

I would expect my string to be split to 2:

['Time:"Last 7 Days"', 'Time:"Last 30 Days"']

but my code splits to 4:

['Time:', '"Last 7 Days"', 'Time:', '"Last 30 Days"']

this is my code:

str.match(/(".*?"|[^"s]+)(?=s*|s*$)/g);
load more v
72%

UPDATE: Sample regex to handle single and double quoted strings. Ref: How can I split on a string except when inside quotes?,If you don't mind having the quotes in the returned list, you can use much simpler code:,I added the capturing groups because you don't want the quotes in the list.,Quoted strings will be group 2, single unquoted words will be group 3.

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 "']+|"([ ^ "]*)" | '([^'] * )
   '
load more v
65%

String str = "Location \"Welcome to india\" Bangalore " +
"Channai \"IT city\" Mysore";

List<String> list = new ArrayList<String>();
      Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
      while (m.find())
      list.add(m.group(1)); // Add .replace("\"", "") to remove surrounding quotes.


      System.out.println(list);
load more v
75%

Many times when you're parsing text you find yourself needing to split strings on a comma character (or new lines, tabs, etc.), but then what if you needed to use a comma in your string and not split on it? An example of this could be a large number. So maybe we'd have a string like this:,So now to split on this we'll need to create a regex string that says "split on all comma characters unless it's in between quotes".,To practice, try looking at the regex we gave and see if you can modify it to split on a different character, like a semicolon (;). If that was easy, try modifying it so it needs to see two quotation marks on each side of the string.,One way to solve this problem is to put quotes around the string that shouldn't be split. So our example from above would then look like this:

Many times when you're parsing text you find yourself needing to split strings on a comma character (or new lines, tabs, etc.), but then what if you needed to use a comma in your string and not split on it? An example of this could be a large number. So maybe we'd have a string like this:

age: 28, favorite number: 26, salary: $1, 234, 108
load more v
40%

preg_split — Split string by a regular expression, Split the given string by a regular expression. ,preg_match() - Perform a regular expression match,preg_quote() - Quote regular expression characters

Array
   (
      [0] => hypertext[1] => language[2] => programming
   )
load more v
22%

I would like to split it by the spaces but the space inside the quote should be ignored. So the splitted strings should be:,Take a look at this question. You may be able to adapt its solution to ignore the spaces in the quotes rather than the commas.,Java: splitting a comma-separated string but ignoring commas in quotes,Instead of splitting, just match things that are not space.

I have a string like this:

"Video or movie"
"parent"
"Media or entertainment"
"1"
"1"
"1"
"0"
"0"

I would like to split it by the spaces but the space inside the quote should be ignored. So the splitted strings should be:

"Video or movie"
"parent"
"Media or entertainment"
"1"
...
load more v

Other "string-split" queries related to "Regular expression to split by space but ignore double-quotes string"