c++ std regex问号问题 [英] c++ std regex question mark issue

查看:34
本文介绍了c++ std regex问号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用标准正则表达式时遇到了麻烦.我无法使问号量词起作用.对 regex_match 的调用将始终返回 0.

I'm having troubles with std regex. I can't make the question mark quantifier work. The call to regex_match will always return 0.

我也尝试过使用 {0,1},它的行为也与我预期的不同:它的行为类似于 + 量词.

I also tried with {0,1} which doesn't behave like I expected either: it behaves like a + quantifier.

这是我的代码:

#include <iostream>
#include <regex>
using namespace std;

int main(int argc, char **argv){

    regex e1("ab?c");
    cout << regex_match("ac", e1) << endl;    // expected : 1, output 0
    cout << regex_match("abc", e1) << endl;   // expected : 1, output 0
    cout << regex_match("abbc", e1) << endl;  // expected : 0, output 0

    regex e2("ab{0,1}c");
    cout << regex_match("ac", e2) << endl;    // expected : 1, output 0
    cout << regex_match("abc", e2) << endl;   // expected : 1, output 1
    cout << regex_match("abbc", e2) << endl;  // expected : 0, output 1

    return 0;
}

我使用以下命令编译:

g++ -std=c++11 main.cpp -o regex_test

我在这里做错了吗?或者为什么它不起作用?

Am i doing something wrong here? Or why isn't it working?

推荐答案

str::regex 大多在 gcc 中实现(在撰写本文时).见第 28 节:http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011

str::regex is mostly not implemented in gcc (at the time of writing). See section 28 at: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011

这篇关于c++ std regex问号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆