Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to split on spaces unless in quotes

Tags:

c#

.net

regex

I would like to use the .Net Regex.Split method to split this input string into an array. It must split on whitespace unless it is enclosed in a quote.

Input: Here is "my string"    it has "six  matches"

Expected output:

  1. Here
  2. is
  3. my string
  4. it
  5. has
  6. six  matches

What pattern do I need? Also do I need to specify any RegexOptions?

like image 650
Shaun Bowe Avatar asked Feb 16 '09 18:02

Shaun Bowe


People also ask

Do I need to escape quotes in regex?

In order to use a literal ^ at the start or a literal $ at the end of a regex, the character must be escaped. Some flavors only use ^ and $ as metacharacters when they are at the start or end of the regex respectively. In those flavors, no additional escaping is necessary. It's usually just best to escape them anyway.

How do you write spaces in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

Which expression is used to match one or more spaces?

The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.


1 Answers

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)); 
like image 143
Bartek Szabat Avatar answered Sep 24 '22 07:09

Bartek Szabat