【发布时间】:2015-03-28 18:55:58
【问题描述】:
我正在尝试使用正则表达式进行组匹配。我想从一个大字符串中提取两个字符串。
输入字符串如下所示:
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也是如此。
我想要做的是提取井号# 之后的用户名。不是来自字符串中的任何其他位置,因为它也会有所不同。我还想从分号 : 之后的字符串中获取 message。
我使用以下正则表达式进行了尝试。但它从不输出任何结果。
regex rgx("WEBMSG #([a-zA-Z0-9]) :(.*?)");
smatch matches;
for(size_t i=0; i<matches.size(); ++i) {
cout << "MATCH: " << matches[i] << endl;
}
我没有得到任何匹配。我的正则表达式有什么问题?
【问题讨论】:
-
是否有必要使用正则表达式来解决这个问题,因为在我看来,流提取功能可以实现这一点。