C++正则表达式
2018-06-06 本文已影响0人
szn好色仙人
基本组件
//定义正则表达式
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
//匹配结果的集合
typedef match_results<const char *> cmatch;
typedef match_results<const wchar_t *> wcmatch;
typedef match_results<string::const_iterator> smatch;
typedef match_results<wstring::const_iterator> wsmatch;
//匹配结果的集合中的元素
typedef sub_match<const char *> csub_match;
typedef sub_match<const wchar_t *> wcsub_match;
typedef sub_match<string::const_iterator> ssub_match;
typedef sub_match<wstring::const_iterator> wssub_match;
//迭代器适配器
typedef regex_iterator<const char *> cregex_iterator;
typedef regex_iterator<const wchar_t *> wcregex_iterator;
typedef regex_iterator<string::const_iterator> sregex_iterator;
typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
regex_match //匹配测试
regex_search //搜索
regex_replace //替换
- 使用示例
#include <regex>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str("12ab34cd");
regex aRex[] =
{
regex("([0-9]+)([a-z]+)"),
regex("(([0-9]+)([a-z]+))+")
};
bool aRe[2] =
{
regex_match(str, aRex[0]), //false
regex_match(str, aRex[1]) //true
};
smatch sm;
bool nRe = regex_search(str, sm, aRex[0]);
string aStrRe[3] =
{
sm.str(0), //[0] = "12ab"
sm.str(1), //[1] = "12"
sm.str(2) //[2] = "ab"
};
string aStrReplace[] =
{
regex_replace(str, aRex[0], string("New")), //[0] = "NewNew"
regex_replace(str, aRex[0], string("New"), //[1] = "New34cd"
std::regex_constants::format_first_only),
regex_replace(str, aRex[0], string("$1|$2"))//[2] = "12|ab34|cd"
};
/*
match_default = 0x0000, //默认
match_not_bol = 0x0001, //不将首字符作为行首处理
match_not_eol = 0x0002, //不将尾字符作为行尾处理
match_not_bow = 0x0004, //不将首字符作为单词首处理
match_not_eow = 0x0008, //不将尾字符作为单词尾处理
match_any = 0x0010, //若存在多个匹配,则返回任意一个匹配
match_not_null = 0x0020, //不匹配任何空序列
match_continuous = 0x0040, //匹配必须从输入的首字符开始
match_prev_avail = 0x0100, //输入序列包含第一个匹配之前的内容
format_default = 0x0000, //用ECMAScript规则替换字符串
format_sed = 0x0400, //用POXIS sed规则替换字符串
format_no_copy = 0x0800, //不输出输入部分中未匹配的部分
format_first_only = 0x1000 //只替换子表达式第一次出现的位置
*/
vector<string> aVecStr[2];
for (sregex_iterator it(str.begin(), str.end(), aRex[0]), itEnd;
it != itEnd; ++it)
{
aVecStr[0].emplace_back(it->str());
aVecStr[1].emplace_back(it->str(1));
}
/*
aVecStr[0]:
[0] = "12ab"
[1] = "34cd"
aVecStr[1]:
[0] = "12"
[1] = "34"
*/
return 0;
}
匹配规则
一般匹配
符号 | 释义 |
---|---|
\ | 转义 |
. | 匹配除\n外的任意字符 |
| | 或 |
[] | 字符集合 |
[^] | 负集合 |
[x-y] | 范围 |
[^x-y] | 负范围 |
\b | 匹配单词边界,数字不算单词边界 |
\B | 匹配非单词边界 |
\d | 等同于[0-9] |
\D | 等同于[^0-9] |
\s | 匹配空白字符 |
\S | 匹配非空白字符 |
\w | 等同于[0-9a-zA-Z_] |
\W | 等同于[^0-9a-zA-Z_] |
regex_match("1", regex("\x31")); //true
regex_match("a1.+-*/-=\\|][{}?/.,!@#$%^&*()_+", regex(".*")); //true
regex_match("\n a1.+-*/-=\\|][{}?/.,!@#$%^&*()_+", regex(".*"));//false
regex_match("1", regex("1|2")); //true
regex_match("1", regex("[12]")); //true
regex_match("a", regex("[^12]")); //true
regex_match("a", regex("[a-z]")); //true
regex_match("1", regex("[^a-z]")); //true
cmatch cm;
regex_search("abcd abc", cm, regex("[a-z]*c\\b")); //true
string str = cm.str(); //str = "abc"
cmatch cm;
regex_search("zyc abcd", cm, regex("[a-z]*c\\B")); //true
string str = cm.str(); //str = "abc"
regex_match("1", regex("\\d")); //true
regex_match("1", regex("\\D")); //false
regex_match("A", regex("\\D")); //true
regex_match(" \f\n\r\t\v", regex("[\\s]{6}")); //true
regex_match(" \f\n\r\t\v", regex("[ \f\n\r\t\v]{6}")); //true
regex_match("a1.+-*/-=\\|][{}?/.,!@#$%^&*()_+", regex("[\\S]+")); //true
regex_match("a_1_A", regex("\\w+")); //true
regex_match(" \r\t\v\f\n", regex("\\W+")); //true
次数匹配
符号 | 释义 |
---|---|
* | 匹配任意次 |
+ | 匹配至少一次 |
? | 匹配至多一次 |
{n} | 精确匹配n次,n非负 |
{n,} | 至少匹配n次,n非负 |
{n,m} | 匹配次数大于等于n,小于等于m,n与m非负 |
? | 当该字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时, 匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串, 而默认的贪婪模式则尽可能多的匹配所搜索的字符串 |
regex_match("", regex("[0-9]*")); //true
regex_match("123", regex("[0-9]*")); //true
regex_match("", regex("[0-9]+")); //false
regex_match("123", regex("[0-9]+")); //true
regex_match("", regex("[0-9]?")); //true
regex_match("123", regex("[0-9]?")); //false
regex_match("123", regex("[0-9]{3}")); //true
regex_match("123", regex("[0-9]{4}")); //false
regex_match("123", regex("[0-9]{1,}")); //true
regex_match("123", regex("[0-9]{1,2}"));//false
cmatch cm;
string str;
regex_search("111", cm, regex("1*?")); //true
str = cm.str(); //str = ""
regex_search("111", cm, regex("1+?")); //true
str = cm.str(); //str = "1"
regex_search("111", cm, regex("1??")); //true
str = cm.str(); //str = ""
regex_search("111", cm, regex("1{2}?"));//true
str = cm.str(); //str = "11"
regex_search("111", cm, regex("1{2,}?"));//true
str = cm.str(); //str = "11"
regex_search("111", cm, regex("1{1,3}?"));//true
str = cm.str(); //str = "1"
子表达式匹配
符号 | 释义 |
---|---|
() | 获取字表达式 |
(?:) | 不获取字表达式 |
(?=) | 正向肯定预查,在任何匹配的字符串开始处匹配查找字符串。 这是一个非获取匹配。预查不消耗字符,也就是说, 在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索, 而不是从包含预查的字符之后开始 |
(?!) | 正向否定预查,在任何匹配的字符串开始处匹配查找字符串。 这是一个非获取匹配 |
?<= | 反向肯定预查,与正向肯定预查类拟,只是方向相反, C++不支持反向预查 |
?<! | 反向否定预查,与正向否定预查类拟,只是方向相反, C++不支持反向预查 |
cmatch cm;
regex_search("abc 123efg", cm, regex("[0-9]{3}([a-z]*)")); //true
string aStr[] =
{
cm.str(0), //[0] = "123efg"
cm.str(1), //[1] = "efg"
};
cmatch cm;
regex re("[0-9]{3}(?:[a-z]*)([0-9]*)");
regex_search("abc 123efg456", cm, re); //true
string aStr[] =
{
cm.str(0), //[0] = "123efg456"
cm.str(1), //[1] = "456"
};
cmatch cm;
regex re("[a-z]*(1|2|3)");
regex_search("abc4 efg1", cm, re); //true
string aStr[] =
{
cm.str(0), //[0] = "efg1"
cm.str(1), //[1] = "1"
};
cmatch cm;
regex re("[a-z]*(?=1|2|3)");
regex_search("abc4 efg1", cm, re); //true
string aStr[] =
{
cm.str(0), //[0] = "efg"
cm.str(1), //[1] = ""
};
cmatch cm;
regex re("[a-z]*(?=1|2|3)([0-9]*)");
regex_search("abc4 efg1", cm, re); //true
string aStr[] =
{
cm.str(0), //[0] = "efg1"
cm.str(1), //[1] = "1"
cm.str(2), //[2] = ""
};
cmatch cm;
regex re("[a-z]*(?!1|2|3)");
bRe = regex_search("abc4 efg1", cm, re); //true
string aStr[] =
{
cm.str(0), //[0] = "abc"
cm.str(1), //[1] = ""
};
位置匹配
符号 | 释义 |
---|---|
^ | 文本的开始 |
$ | 文本的结尾 |
cmatch cm;
regex_search("123 abc", cm, regex("^[a-z]+")); //false
regex_search("123 abc efg", cm, regex("[a-z]+$")); //true
string str = cm.str(); //str = "efg"