Regex to parse a string into tokens, like the command-line

The following regex is useful to parse a line into its parts, separated by spaces, unless there are quotes, in which case the quotes take precedence. 

 

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

 

as a C# string it should be like this @"(\"[^\"]*\")|([^\s]+)", but I haven't tested it within code yet.

I've only tested this with the excellent regex tool 'Expresso', from http://www.ultrapico.com, for building and testing Regular Expressions.  It is written with .NET, so it presumably uses the .NET regex engine.

Print | posted on Monday, January 26, 2009 9:55 AM

Feedback

# re: Regex to parse a string into tokens, like the command-line

left by hdub at 1/8/2010 8:15 PM Gravatar
Thanks! Saved me a couple hours
Title  
Name
Email (never displayed)
Url
Comments   
Please add 7 and 1 and type the answer here: