基本組件
//定義正則表達(dá)式
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
//匹配結(jié)果的集合
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;
//匹配結(jié)果的集合中的元素
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, //默認(rèn)
match_not_bol = 0x0001, //不將首字符作為行首處理
match_not_eol = 0x0002, //不將尾字符作為行尾處理
match_not_bow = 0x0004, //不將首字符作為單詞首處理
match_not_eow = 0x0008, //不將尾字符作為單詞尾處理
match_any = 0x0010, //若存在多個(gè)匹配锭碳,則返回任意一個(gè)匹配
match_not_null = 0x0020, //不匹配任何空序列
match_continuous = 0x0040, //匹配必須從輸入的首字符開始
match_prev_avail = 0x0100, //輸入序列包含第一個(gè)匹配之前的內(nèi)容
format_default = 0x0000, //用ECMAScript規(guī)則替換字符串
format_sed = 0x0400, //用POXIS sed規(guī)則替換字符串
format_no_copy = 0x0800, //不輸出輸入部分中未匹配的部分
format_first_only = 0x1000 //只替換子表達(dá)式第一次出現(xiàn)的位置
*/
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;
}
匹配規(guī)則
一般匹配
符號 | 釋義 |
---|---|
\ | 轉(zhuǎn)義 |
. | 匹配除\n外的任意字符 |
| | 或 |
[] | 字符集合 |
[^] | 負(fù)集合 |
[x-y] | 范圍 |
[^x-y] | 負(fù)范圍 |
\b | 匹配單詞邊界阵漏,數(shù)字不算單詞邊界 |
\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
次數(shù)匹配
符號 | 釋義 |
---|---|
* | 匹配任意次 |
+ | 匹配至少一次 |
? | 匹配至多一次 |
{n} | 精確匹配n次,n非負(fù) |
{n,} | 至少匹配n次,n非負(fù) |
{n,m} | 匹配次數(shù)大于等于n,小于等于m,n與m非負(fù) |
? | 當(dāng)該字符緊跟在任何一個(gè)其他限制符(*,+,?,{n},{n,},{n,m})后面時(shí), 匹配模式是非貪婪的。非貪婪模式盡可能少的匹配所搜索的字符串迈着, 而默認(rèn)的貪婪模式則盡可能多的匹配所搜索的字符串 |
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"
子表達(dá)式匹配
符號 | 釋義 |
---|---|
() | 獲取字表達(dá)式 |
(?:) | 不獲取字表達(dá)式 |
(?=) | 正向肯定預(yù)查,在任何匹配的字符串開始處匹配查找字符串邪码。 這是一個(gè)非獲取匹配裕菠。預(yù)查不消耗字符,也就是說闭专, 在一個(gè)匹配發(fā)生后奴潘,在最后一次匹配之后立即開始下一次匹配的搜索, 而不是從包含預(yù)查的字符之后開始 |
(?!) | 正向否定預(yù)查影钉,在任何匹配的字符串開始處匹配查找字符串画髓。 這是一個(gè)非獲取匹配 |
?<= | 反向肯定預(yù)查,與正向肯定預(yù)查類擬平委,只是方向相反奈虾, C++不支持反向預(yù)查 |
?<! | 反向否定預(yù)查,與正向否定預(yù)查類擬,只是方向相反肉微, C++不支持反向預(yù)查 |
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] = ""
};
位置匹配
符號 | 釋義 |
---|---|
^ | 文本的開始 |
$ | 文本的結(jié)尾 |
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"