Reputation: 495
This pattern keeps giving me errors as if it is not backing out of the double quotes. I am trying to grab "Gen"
string str = "<div type=\"book\" osisID=\"Gen\">";
Match m = Regex.Match(str, @"<div type=\"book\" osisID=\"(.*?)\">", RegexOptions.IgnoreCase);
if (m.Success) {
Console.Write(m.Groups[1].Value);
}
Upvotes: 0
Views: 1537
Reputation: 100288
Use XML parsing mechanism to parse XML:
var doc = XDocument.Parse(xml)
var root = doc.Root
var osisId = root.Attribute("osisID").Value;
Upvotes: 3
Reputation: 35353
Assuming you have more complex html than you've just posted and have already read this
string str = "<div type=\"book\" osisID=\"Gen\">";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(str);
var osisID = doc.DocumentNode
.SelectSingleNode("//div[@type='book']")
.Attributes["osisID"]
.Value;
PS: HtmlAgilityPack
Upvotes: 1
Reputation: 75242
In C# verbatim strings, you escape a quotation mark with another quotation mark, not with a backslash:
@"<div type=""book"" osisID=""(.*?)"">"
Upvotes: 3