[英]Why doesn't std::regex_match return true with $ pattern?
使用std :: regex_replace ,
我可以使用“ $”匹配單詞的結尾,然后附加其他字符。
std::cout << std::regex_replace("word",std::regex("$"),"s") << '\n';
//prints: words
由於結果與原始輸入不同,因此我認為正則表達式已匹配。
然而,
std::cout << std::boolalpha;
std::cout << std::regex_match("word",std::regex("$")) << '\n';
//prints: false
正則表達式不匹配怎么可能,但是regex_replace能夠進行替換?
我還嘗試為0個或更多字符添加*
,但這引發了異常:
std::cout << std::regex_match("word",std::regex("*$")) << '\n';
std::cout << std::regex_replace("word",std::regex("*$"),"s") << '\n';
請注意,regex_match將僅將正則表達式成功匹配到整個字符序列,而std :: regex_search將成功匹配子序列。
您需要regex_search
匹配的子序列。 $
與非空字符串的整體不匹配,但與非空字符串的(零長度)子字符串匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.