C ++ Regex doesn't match multi-line strings

I am having problems with C ++ 0x regex when the string match Im is a multi-line string. Here is a snippet of Im code that is trying to use:

std::smatch regMatch;
std::string data = "<key>id</key><string>1</string>\n<key>user</key><string>admin</string>";
if (std::regex_match(data, regMatch, std::regex("<key>user</key><string>(.*?)</string>"))) {
    std::cout << "Reg match: " << regMatch[1].str() << std::endl;
}
+4
source share
2 answers

You should use regex_searchinstead regex_match.

By the way, why not use (.*)instead (.*?)?

+6
source

The .default dot does not match newlines. You can add a switch (?s)to the beginning of the regular expression to enable newline matching for the period:

(?s)<key>user</key><string>(.*?)</string>

, . , , . , , , \s \w . , , :

<key>user</key><string>([\w\W]*?)</string>

, , XML , , , "", ( ).

admin$#* &% '"; _____?

+3

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


All Articles