c++ - Bug in VS13 regex: wrong order for alternatives? -


i need regex captures argument between parentheses. blanks before , after argument should not captured. example, "( ab & c )" should return "ab & c". argument can enclosed single quotes if leading or trailing blanks needed. so, "( ' ab & c ' )" should return " ab & c ".

wstring string = l"( ' ab & c ' )"; wsmatch matches; regex_match( string, matches, wregex(l"\\(\\s*(?:'(.+)'|(.+?))\\s*\\)") ); wcout << l"<" + matches[1].str() + l"> " + l"<" + matches[2].str() + l">" + l"\n"; // results in "<> < ' ab & c '>", not ok 

it seems second alternative matched, took space in front of first quote! should have been caught \s after opening parenthesis.

removing second alternative:

regex_match( string, matches, wregex(l"\\(\\s*(?:'(.+)')\\s*\\)") ); wcout << l"<" + matches[1].str() + l">" + l"\n"; // results in "< ab & c >", ok 

making capturing group of alternatives:

regex_match( string, matches, wregex(l"\\(\\s*('(.+)'|(.+?))\\s*\\)") ); wcout << l"<" + matches[1].str() + l"> " + l"<" + matches[2].str() + l"> " + l"<" + matches[3].str() + l">" + l"\n"; // results in "<' ab & c '> < ab & c > <> ", ok 

am overlooking anything?

here suggestion merges 2 alternatives 1:

wstring string = l"( ' ab & c ' )"; wsmatch matches; regex_match( string, matches, wregex(l"\\(\\s*(')?([^']+)\\1\\s*\\)") ); wcout << l"<" + matches[2].str() + l"> " + l"\n"; 

the \(\s*(')?([^']+)\1\s*\) regex using back-reference make sure have ' @ beginning , end in order not capture 'something. value caught group 2.

output:

enter image description here


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -