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
  • 95,331
  • 80
  • 293
  • 426
user1898657
  • 485
  • 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
  • 95,331
  • 80
  • 293
  • 426
3

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

 @"<div type=""book"" osisID=""(.*?)"">"
Alan Moore
  • 71,299
  • 12
  • 93
  • 154
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,225
  • 4
  • 65
  • 78