I have a string:
<graphic id="8374932">Translating Cowl (Inner/Outer Bondments</graphic>
And my pattern:
"<graphic id=\"(.*?)\">(.*?)</graphic>"
But it fails for second group, saying: "Not enough )'s." How should I prevent it?
EDIT: First off, if you goal is to parse HTML or XML I strongly advise against it. If your goal is to learn or to surgically grab an element node then regex may, and I say may be a tool to use. I am answering this with the thought that you are using the html pattern to learn from....
I believe you have confused your data with your pattern and the regex pattern is failing.
I recommend these things
How to get the text? Tell the regex parser to get everthing that is not an anchor character by using the set operation with the ^ (which means not when in a set [ ]) such as ([^\"]+) which says match everything that is not a quote.
Change your pattern to this which demonstrates the above suggestions:
string data = @"<graphic id=""8374932"">Translating Cowl (Inner/Outer Bondments</graphic>";
// \x22 is the hex escape for the quote, makes it easier to read.
string pattern = @"
(?:graphic\s+id=\x22) # Match but don't capture (MBDC) the beginning of the element
(?<ID>[^\x22]+) # Get all that is not a quote
(?:\x22>) # MBDC the quote
(?<Content>[^<+]+) # Place into the Content match capture group all text that is not + or <
(?:\</graphic) # MBDC The graphic";
// Ignore Pattern whitespace only allows us to comment, does not influence regex processing.
var mt = Regex.Match(data, pattern, RegexOptions.IgnorePatternWhitespace);
Console.WriteLine ("ID: {0} Content: {1}", mt.Groups["ID"], mt.Groups["Content"]);
// Outputs:
// ID: 8374932 Content: Translating Cowl (Inner/Outer Bondments
.Group[2]?)...inputandpatternparameters?