开发者

Get string between quotes using regex

开发者 https://www.devze.com 2023-04-05 00:28 出处:网络
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:

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开发者_如何学JAVA.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#?


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.


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\">";


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

0

精彩评论

暂无评论...
验证码 换一张
取 消