VS13 regex error: wrong order for alternatives?

I need a regular expression that captures an argument between parentheses. Billets before and after the argument must not be captured. For example, "( ab & c )" should return "ab & c" . The argument can be enclosed in single quotes if leading or trailing spaces are 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 

The second alternative seems to fit, but it also took up space before the first quote! It should have been caught \s after opening the parenthesis.

Removing the second alternative:

 regex_match( String, Matches, wregex(L"\\(\\s*(?:'(.+)')\\s*\\)") ); wcout << L"<" + Matches[1].str() + L">" + L"\n"; // Results in "< ab & c >", OK 

Creating an alternate capture group:

 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 I not noticing anything?

+6
source share
1 answer

Here is my suggestion that combines the two alternatives in 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 regular expression \(\s*(')?([^']+)\1\s*\) uses a backlink to make sure that we have ' at the beginning and at the end, so as not to write 'something . The value falls into group 2.

Output:

enter image description here

+1
source

Source: https://habr.com/ru/post/986229/


All Articles