I'm pretty sure the match[1]
result I get in Visual Studio 2010 is due to a bug.
In Visual Studio 2012 and 2013 and gcc 4.9.0 (20140405) the code returns what I expect:
match.length(): 21
match[0]: "line 1
line 2
line 3
"
match[1]: "line 1
line 2
"
match[2]: "3"
Online regular expression testers RegExr and Regex Hero show the same thing.
In Visual Studio 2010 to make the expression work properly I can make it "lazy" by adding a question mark after the plus sign: "^([^\\0]+?\\n)?line (3)\\n"
. (That's a string literal so each backslash is escaped with a backslash.) Although it works now (but differently since it's now finding the closest match since it's lazy) I'm sure it's better to just use the latest Visual Studio.
clang-503.0.40 has a different but related bug where it can't process "[^\0]*".