问题描述
我在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!";
有人知道这样吗?
推荐答案
使用,而不是。后者尝试匹配整个主题字符串,而前者允许匹配子字符串。
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正则表达式子串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!