William Smith
William Smith

Reputation: 1959

Get string between quotes using regex

I have a string that is basically an XML node, and I need to extract the values of the attributes. I am trying to use the following C# code to accomplish this:

string line = "<log description="Reset Controls - MFB - SkipSegment = True" start="09/13/2011 10:29:58" end="09/13/2011 10:29:58" timeMS="0" serviceCalls="0">"
string pattern = "\"[\\w ]*\"";
Regex r = new Regex(pattern);

foreach (Match m in Regex.Matches(line, pattern))
{
    MessageBox.Show(m.Value.Substring(1, m.Value.Length - 2));
}

The problem is that this is only returning the last occurrence from the string ("0" in the above example), when each string contains 5 occurrences. How do I get every occurrence using C#?

Upvotes: 0

Views: 1700

Answers (3)

Scott Rippey
Scott Rippey

Reputation: 15810

To actually answer your question, your pattern should probably be "\"[^\"]*\""because \w won't match spaces, symbols, etc.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500515

Don't try to parse XML with regular expressions. Use an XML API instead. It's simply a really bad idea to try to hack together "just enough of an XML parser" - you'll end up with incredibly fragile code.

Now your line isn't actually a valid XML element at the moment - but if you add a </log> it will be.

XElement element = XElement.Parse(line + "</log>");
foreach (XAttribute attribute in element.Attributes())
{
    Console.WriteLine("{0} = {1}", attribute.Name, attribute.Value);
}

That's slightly hacky, but it's better than trying to fake XML parsing yourself.

Upvotes: 6

Jonathan M
Jonathan M

Reputation: 17451

As an aside, you need to escape your string for double-quotes and add a semi-colon:

string line = "<log description=\"Reset Controls - MFB - SkipSegment = True\" start=\"09/13/2011 10:29:58\" end=\"09/13/2011 10:29:58\" timeMS=\"0\" serviceCalls=\"0\">";

Upvotes: 1

Related Questions