3
我有一點C++ 11 RegEx的問題,我認爲它是關於貪婪。C++ 11正則表達式,非貪婪
這是一個小樣本。
#include <stdio.h>
#include <string>
#include <regex>
int main (void)
{
std::string in="{ab}{cd}[ef]{gh}[ij][kl]"; // the input-string
std::regex rx1 ("(\\{.+?})(.*)", std::regex::extended); // non-greedy?
std::smatch match;
if (regex_match (in, match, rx1))
{
printf ("\n%s\n", match.str(1).c_str());
}
return 0;
}
我希望
{ab}
輸出。 但我得到
{ab}{cd}[ef]{gh}
我期望的結果我得到的,如果我貪心,但不與做呢?之後。+。 應該讓它不貪心吧?
那麼,我的想法是什麼問題? 感謝您的幫助!
克里斯
爲什麼你逃跑用'\\ {'但是你不要逃右大括號的大括號? – Bobulous
@Bobulous這是一個正則表達式的特殊字符,它有助於重複以前的模式。 –
我認爲你不應該使用'std :: regex :: extended',它使你的正則表達式符合POSIX ERE,不支持懶惰量詞。 –