加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用正则表达式c?

发布时间:2020-12-14 05:48:49 所属栏目:百科 来源:网络整理
导读:我有一个c正则表达式的问题. 我有一个像“f 123/123 1234/123/124 12”这样的字符串,我希望在“/”之前得到所有数字. 这是我的正则表达式: “s(d+)”. 我在http://rubular.com/上试过它并且它有效. 这是我的代码: std::regex rgx("s(d+)");std::smat
我有一个c正则表达式的问题.
我有一个像“f 123/123 1234/123/124 12”这样的字符串,我希望在“/”之前得到所有数字.

这是我的正则表达式:

“s(d+)”.

我在http://rubular.com/上试过它并且它有效.
这是我的代码:

std::regex rgx("s(d+)");
std::smatch match;

if (std::regex_search(line,match,rgx))
{
    std::cout << "Matchn";

    std::string *msg = new std::string();
    std::cout << "match[0]:" << match[0] << "n";
    std::cout << "match[1]:" << match[1] << "n";
    std::cout << "match[2]:" << match[2] << "n";

}

但我明白了:

Match
match[0]:123
match[1]:123
match[2]:

我意识到match [0]不是像Python和Ruby那样的“组”.

解决方法

regex_search匹配一次(与正则表达式匹配的第一个子字符串).你需要循环.

试试以下:

std::regex rgx("s(d+)");
std::smatch match;

while (std::regex_search(line,rgx))
{
    std::cout << match[0] << std::endl;
    line = match.suffix().str();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读