Eva*_*nED 9 c++ regex boost boost-regex c++11
我正在使用C++ 11的<regex>
支持,并想检查字符串的开头是否与正则表达式匹配.[如果有帮助我可以切换到Boost,但我的印象是它们基本相同.]
显然,如果我能控制表达式的实际文本表示,我可以^
在它的开头作为锚点.
但是,如果我只有一个regex
(或basic_regex
)对象怎么办?我可以修改它代表的正则表达式来添加锚吗?或者我必须使用regex_search
,得到结果,并检查它是否从0位开始?
ken*_*ytm 11
您可以std::regex_constants::match_continuous
在使用时添加标志regex_search
,例如,以下打印"1"和"0":
#include <regex>
#include <string>
int main()
{
std::regex rx ("\\d+");
printf("%d\n", std::regex_search("12345abc1234", rx,
std::regex_constants::match_continuous));
printf("%d\n", std::regex_search("abc12345", rx,
std::regex_constants::match_continuous));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
标志意味着(C++11§28.5.2/ 1 =表139):
表达式只能匹配以...开头的子序列
first
.
归档时间: |
|
查看次数: |
3785 次 |
最近记录: |