美文网首页
正则表达式 2020-07-30

正则表达式 2020-07-30

作者: liyangyao | 来源:发表于2020-08-03 16:47 被阅读0次

元字符

. 除换行符(\n、\r)之外的任何单个字符
\d [0-9]
\w [A-Za-z0-9_]
\s 匹配任何空白字符

定位

^ 行首
$ 行尾
\b 单词边界

分组

(ab)*
(a|b)*

区间

[0-9]
[A-Za-z0-9_]
[一-龥!-~] 中文和中文符号

重复

{n} 重复n次
{n,} 重复>=n次
{n,m} n次<=重复<=m次
* 0次或者多次 {0,}
+ 1次或者多次 {1,}
? 0次或者1次 {0,1}

编程

#include <regex>
{//匹配
    string s1 = "ab123cdef";
    string s2 = "123456789";

    regex ex("\\d+");

    qDebug() << s1 << " is all digit: " << regex_match(s1, ex);
    qDebug() << s2 << " is all digit: " << regex_match(s2, ex);
}

{//搜索
    string s = "ab123cdef";
    regex ex("\\d+");
    smatch match;
    regex_search(s, match, ex);
    qDebug() << s << " contains digit: " << match[0];
}

{//替换
    string s = "ab123cdef";
    regex ex("\\d+");

    string r = regex_replace(s, ex, "xxx");
    qDebug() << r;
}

相关文章

网友评论

      本文标题:正则表达式 2020-07-30

      本文链接:https://www.haomeiwen.com/subject/evvrrktx.html