简体   繁体   中英

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#?

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.

顺便说一句,您需要将字符串转义为双引号并添加分号:

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

要实际回答您的问题,您的pattern应该为"\\"[^\\"]*\\""因为\\w将不匹配空格,符号等。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM