关于C++11的正则,到底该怎么用?

elemusic 2016-01-26 03:55:45
我想匹配一个字符串,到bmp结束,也就是?????.bmp这种,后面的东西全都丢掉。

C++11代码如下


#include <iostream>
#include <regex>

using namespace std;

int main()
{
string name = R"(\\Graphics\\00_Character\\Attack_A.bmp&@#(*_^#@^)*(&!_(*~#&%*#%)";
regex e(".*?bmp");

bool bMatched = false;
bMatched = regex_match(name, e);

cout << bMatched << endl;

return 0;
}


但是不论是VS2013还是G++都不行。

但是用其他语言,比如AS3或者在线的工具,比如下面这个,就没问题,请问是哪的问题?
是C++11的正则与其他正则不同,还是怎么得?

http://tool.oschina.net/regex/

...全文
229 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-01-27
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
    string str("This expression could match from A and beyond. [ expression  express ]");
    string rs = "exp\\w*";  // 正则字符串,exp开始的单词
    cout << str << endl;

    regex expression(rs); // 字符串传递给构造函数,建立正则表达式

// regex_match 判断一个正则表达式(参数 e)是否匹配整个字符序列 str. 它主要用于验证文本。
// 注意,这个正则表达式必须匹配被分析串的全部,否则函数返回 false.
// 如果整个序列被成功匹配,regex_match 返回 True.
    bool ret = regex_match(str, expression);
    if (ret)
        cout << "可以匹配整个文本" << endl;
    else
        cout << "不能匹配整个文本" << endl;

// regex_search 类似于 regex_match, 但它不要求整个字符序列完全匹配。
// 你可以用 regex_search 来查找输入中的一个子序列,该子序列匹配正则表达式 e.
    ret = regex_search(str, expression);
    if (ret)
        cout << "能够搜索到 " << rs << endl;
    else
        cout << "不能搜索" << endl;

// regex_replace 在整个字符序列中查找正则表达式e的所有匹配。
// 这个算法每次成功匹配后,就根据参数fmt对匹配字符串进行格式化。
// 缺省情况下,不匹配的文本不会被修改,即文本会被输出但没有改变。

    str = regex_replace(str, expression, string("表达式"));
// VC2010或者TR1注意:这里第三个参数要用string()转换
// http://www.johndcook.com/cpp_regex.html
// started with C++ TR1 regular expressions
    cout << str << endl;
    return 0;
}
//This expression could match from A and beyond. [ expression  express ]
//不能匹配整个文本
//能够搜索到 exp\w*
//This 表达式 could match from A and beyond. [ 表达式  表达式 ]
elemusic 2016-01-27
  • 打赏
  • 举报
回复
顶一次,果然C++用户都不用正则,没人讨论么?明天没人回就结贴了
D41D8CD98F 2016-01-27
  • 打赏
  • 举报
回复
regex_search regex_match regex_replace 一共就三个函数,想怎么讨论
  • 打赏
  • 举报
回复
    std::string subject = R"(\\Graphics\\00_Character\\Attack_A.bmp&@#(*_^#@^)*(&!_(*~#&%*#%)";
    try
    {
        std::regex re( ".*?bmp", std::regex_constants::icase );
        std::smatch match;

        if ( std::regex_search( subject, match, re ) )
        {
            std::cout << match[0] << std::endl;
        }
        else
        {
        }
    }
    catch ( std::regex_error& e )
    {
    }
elemusic 2016-01-26
  • 打赏
  • 举报
回复
我知道可以蛮力写匹配,但是这贴主要是想问下C++11的正则为什么和其他语言的正则不一样,请讨论正则的问题,谢谢您的合作
dustpg 2016-01-26
  • 打赏
  • 举报
回复
http://en.cppreference.com/w/cpp/regex/regex_match 找找每个函数用法自然就清楚了
赵4老师 2016-01-26
  • 打赏
  • 举报
回复
没必要用正则表达式:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char s[]="(\\Graphics\\00_Character\\Attack_A.bmp&@#(*_^#@^)*(&!_(*~#&%*#%)";
char t[100];
char *p;
int main() {
    p=strstr(s,".bmp");
    if (p) {
        strncpy(t,s,__min(99,p-s+4));t[99]=0;
        printf("%s\n",t);//(\Graphics\00_Character\Attack_A.bmp
    }
    return 0;
}
iyomumx 2016-01-26
  • 打赏
  • 举报
回复
用regex_search代替regex_match 另外g++默认使用的libstdc++库可能没有实现正则表达式部分

64,640

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧