user1898657
user1898657

Reputation: 495

RegEx matching double quotes

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

Answers (3)

abatishchev
abatishchev

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

I4V
I4V

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

Alan Moore
Alan Moore

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

Related Questions