加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式与C regex_match无法正常工作

发布时间:2020-12-14 06:31:54 所属栏目:百科 来源:网络整理
导读:我正在研究c 11中的正则表达式,这个正则表达式搜索返回false.有谁知道我在做错了什么? .我知道.*代表除换行符之外的任意数量的字符. 所以我期待regex_match()返回true并将输出“找到”. 然而,输出结果是“未找到”. #includeregex#includeiostreamusing nam
我正在研究c 11中的正则表达式,这个正则表达式搜索返回false.有谁知道我在做错了什么? .我知道.*代表除换行符之外的任意数量的字符.

所以我期待regex_match()返回true并将输出“找到”.
然而,输出结果是“未找到”.

#include<regex>
#include<iostream>

using namespace std;

int main()
{
    bool found = regex_match("<html>",regex("h.*l"));// works for "<.*>"
    cout<<(found?"found":"not found");
    return 0;
}
您需要使用regex_search而不是regex_match:
bool found = regex_search("<html>",regex("h.*l"));

见IDEONE demo

简单来说,regex_search将在给定字符串中的任何位置搜索子字符串.如果整个输入字符串匹配,则regex_match将仅返回true(与Java中的匹配行为相同).

regex_match文件说:

Returns whether the target sequence matches the regular expression rgx.
The entire target sequence must match the regular expression for this function >to return true (i.e.,without any additional characters before or after the >match). For a function that returns true when the match is only part of the >sequence,see regex_search.

regex_search是不同的:

Returns whether some sub-sequence in the target sequence (the subject) matches the regular expression rgx (the pattern).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读