Yes thanks. Here is a ~better one that will escape "\" anything, including
a quote inside quote pairs:
public static string[] SplitQuoted(string text, string seperators)
{
// "([^"\\]*(\\.[^"\\]*)*)"
// |
// ([^\s,]+)
// Default seperators is a space and tab (e.g. " \t").
// All seperators not inside quote pair are ignored.
// Default quotes pair is two double quotes ( e.g. '""' ).
if ( text == null )
throw new ArgumentNullException("text", "text is null.");
if ( seperators == null || seperators.Length < 1 )
seperators = " \t"; // Default is space and tab.
// if ( quotes == null || quotes.Length < 1 )
// quotes = "\"\"";
ArrayList res = new ArrayList();
// Get the open and close chars, escape them for use in regular
expressions.
// string openChar = Regex.Escape(quotes[0].ToString());
// string closeChar = Regex.Escape(quotes[quotes.Length - 1].ToString());
// Build the pattern that searches for both quoted and unquoted elements
// notice that the quoted element is defined by group #2
// and the unquoted element is defined by group #3.
//| \s*("([^"]*)"|([^,]+))\s* |
// match any spaces upto first quote. that does not contain zero or more
" chars
// ending in a quote OR not one or more commas
// string pattern = @"\s*(" + openChar + "([^" + closeChar + "]*)" +
// closeChar + @"|([^" + seperators + @"]+))\s*";
//"([^"\\]*[\\.[^"\\]*]*)" //Note quotes at either end are required.
//|
//([^\s,]+)
//string[] sa = Regex.Split("my string", "pattern");
string pattern =
@"""([^""\\]*[\\.[^""\\]*]*)""" +
"|" +
@"([^" + seperators + @"]+)";
// Search the string.
foreach ( System.Text.RegularExpressions.Match m in
System.Text.RegularExpressions.Regex.Matches(text, pattern) )
{
//string g0 = m.Groups[0].Value;
string g1 = m.Groups[1].Value;
string g2 = m.Groups[2].Value;
if ( g2 != null && g2.Length > 0 )
{
res.Add(g2);
}
else
{
// get the quoted string, but without the quotes in g1;
res.Add(g1);
}
}
return (string[])res.ToArray(typeof(string));
}
--
William Stacey, MVP
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:br**************@cpmsftngxa10.phx.gbl...
Hi William,
I am glad you got what you want. Do you still have any concern on this
issue?
Please feel free to feedback. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.