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\02.?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
總結
- 上一篇: 压测—ab
- 下一篇: Google开源项目风格指南-笔记