[英]Regex grouping matches with C++ 11 regex library
我正在嘗試使用正則表達式進行組匹配。 我想從一個大字符串中提取兩個字符串。
輸入字符串如下所示:
tХB:Username!Username@Username.tcc.domain.com Connected
tХB:Username!Username@Username.tcc.domain.com WEBMSG #Username :this is a message
tХB:Username!Username@Username.tcc.domain.com Status: visible
Username
可以是任何東西。 結尾部分this is a message
如此, this is a message
。
我想要做的是提取井號#
之后的用戶名。 不是來自字符串中的任何其他位置,因為它也會有所不同。 我還想從分號:
之后的字符串中獲取消息。
我用以下正則表達式嘗試過。 但它永遠不會輸出任何結果。
regex rgx("WEBMSG #([a-zA-Z0-9]) :(.*?)");
smatch matches;
for(size_t i=0; i<matches.size(); ++i) {
cout << "MATCH: " << matches[i] << endl;
}
我沒有得到任何匹配。 我的正則表達式有什么問題?
您的正則表達式不正確,因為捕獲組都沒有做您想要的。 第一個是從集合[a-zA-Z0-9]
匹配單個字符,然后是<space>:
,它適用於單個字符的用戶名,但沒有別的。 第二個捕獲組將始終為空,因為您正在尋找零個或多個字符,但也指定匹配不應貪婪,這意味着零字符匹配是有效結果。
修復這兩個你的regex
變成
std::regex rgx("WEBMSG #([a-zA-Z0-9]+) :(.*)");
但是簡單地實例化一個regex
和一個match_results
對象不會產生匹配,你需要應用一個regex
算法。 由於您只想匹配輸入字符串的一部分,因此在這種情況下使用的適當算法是regex_search
。
std::regex_search(s, matches, rgx);
把這一切放在一起
std::string s{R"(
tХB:Username!Username@Username.tcc.domain.com Connected
tХB:Username!Username@Username.tcc.domain.com WEBMSG #Username :this is a message
tХB:Username!Username@Username.tcc.domain.com Status: visible
)"};
std::regex rgx("WEBMSG #([a-zA-Z0-9]+) :(.*)");
std::smatch matches;
if(std::regex_search(s, matches, rgx)) {
std::cout << "Match found\n";
for (size_t i = 0; i < matches.size(); ++i) {
std::cout << i << ": '" << matches[i].str() << "'\n";
}
} else {
std::cout << "Match not found\n";
}
"WEBMSG #([a-zA-Z0-9]) :(.*?)"
此正則表達式將僅匹配包含 1 個字符長度的用戶名和分號后的任何消息的字符串,但第二組將始終為空,因為嘗試找到從 0 到無限制的任何字符的非貪婪匹配較少。
這應該有效:
"WEBMSG #([a-zA-Z0-9]+) :(.*)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.