0

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);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
user1898657
  • 495
  • 2
  • 5
  • 14

3 Answers3

3

Use XML parsing mechanism to parse XML:

var doc = XDocument.Parse(xml)
var root = doc.Root
var osisId = root.Attribute("osisID").Value;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
3

In C# verbatim strings, you escape a quotation mark with another quotation mark, not with a backslash:

 @"<div type=""book"" osisID=""(.*?)"">"
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
1

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

Community
  • 1
  • 1
I4V
  • 34,891
  • 6
  • 67
  • 79