RE2—C++

参考:

正则表达式(Regular Expression) — Chuanqi 的技术文档

"\"
\\\
.\.
汉字[\p{Han}]

一、函数细节

1. GlobalReplace()

RE2::GlobalReplace(str, pat, new_sub_str ):将句子str中匹配到的子串替换为new_sub_str 

std::string aInput = "~/Test (Folder)/";
RE2::GlobalReplace( &aInput, "(<|>|\\||\\:|\\(|\\)|&|;|\\s)", "\\\\0" );


~/Test\ \(Folder\)/
替换为
~/Test\0\0Folder\0

2. PartialMatch() 

RE2::PartialMatch("hello:1234", pattern, &s, &i):将匹配的组内容放到s、i中
RE2 pattern("(\\w+):(\\d+)", RE2::Quiet);
assert(RE2::PartialMatch("hello:1234", pattern, &s, &i));  // 成功匹配
#include <re2/re2.h>
#include <iostream>
#include <assert.h>
 
int
main(void)
{
    int i;
    std::string s;
    assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &s, &i));
    assert(s == "ruby");
    assert(i == 1234);
 
    // Fails: "ruby" cannot be parsed as an integer.
    assert(!RE2::FullMatch("ruby", "(.+)", &i));
 
    // Success; does not extract the number.
    assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &s));
 
    // Success; skips NULL argument.
    assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", (void*)NULL, &i));
 
    // Fails: integer overflow keeps value from being stored in i.
    assert(!RE2::FullMatch("ruby:123456789123", "(\\w+):(\\d+)", &s, &i));
 
    std::cout << "Ok" << std::endl;
    return 0;
}

3. re2::StringPiece

RE2返回的是匹配的字符串,如果需要知道匹配的偏移量,则将结果存储在re2::StringPiece而不是std::string中, .data()的值将指向原始字符串。

考虑这个程序。
在每个测试中,result.data()是指向原始const char*std::string的指针。

二、Options设置

set_literal (3)

set_posix_syntax (3)

set_perl_classes (3)

set_log_errors (3)

set_case_sensitive (3):大小写

Copy (2)

set_word_boundary (2)

set_never_nl (2)

set_max_mem (2)

set_one_line (1):"^$"开关可用

三、正则语法细节 

配置文件:

(?P<test>电话)       (:|:|是)        (?:[0-9]+)

  匹配获取                                    匹配不获取

(?P<test>电话)(:|:|是)(?:[0-9]+)    test
[0-9]+    num

测试:电话是120

匹配结果:电话—test,120—num。匹配不获取影响后面num

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满腹的小不甘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值