C ++ 11正则表达式子串匹配 [英] C++11 regex substring matching

查看:144
本文介绍了C ++ 11正则表达式子串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ 11中寻找可以匹配字符串中的子字符串的正则表达式。

I´m looking for a regular expression in C++11 which can match a substring in a string.

类似于:这是一个黑暗的夜晚...和我正在寻找 ark

Something like: "It´s a dark night out there..." and I´m searching for ark

所以如果一个子字符串在字符串中的某个位置,这个表达式应该匹配。
此外,我想有一个选项,说这个匹配应区分大小写或不。

So if a substring is somewhere in a string this expression shall match. Furthermore i would like to have the option to say that this match should be case sensitive or not.

我已经尝试过,但它没有似乎工作...

I´ve already tried this but it doesn´t seem to work...

string str = "It´s a dark night out there...";
regex ex ("ark"); 
if (regex_match (str,ex))
    cout << "Match found!";

有人知道这样吗?

推荐答案

使用 regex_search ,而不是 regex_match 。后者尝试匹配整个主题字符串,而前者允许匹配子字符串。

Use regex_search instead of regex_match. The latter tries to match the entire subject string, while the former allows to match substrings.

这里是可能的标志,其中之一(第一个)表示不区分大小写的匹配。

And here are the possible flags, one of which (the first) represents case-insensitive matching.

这篇关于C ++ 11正则表达式子串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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