C++|正则表达式的match、search、replace

正则表达式是一种用于字符串处理的微型语言。

C++支持好几种不同的正则表达式语法,默认语法是ECMAScript。

1 regex_match(string, regex)

//通过正则表达式验证日期输入格式,如2012/11/11
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
\tregex r("\\\\d{4}/(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[1-2][0-9]|3[0-1])");
\twhile (true) {
\t\tcout << "Enter a date (year/month/day) (q=quit): ";
\t\tstring str;
\t\tif (!getline(cin, str) || str == "q")
\t\t\tbreak;
\t\tif (regex_match(str, r))
\t\t\tcout << " Valid date." << endl;
\t\telse
\t\t\tcout << " Invalid date!" << endl;
\t}
\treturn 0;
}
/*
Enter a date (year/month/day) (q=quit): 2012/12/12
Valid date.
Enter a date (year/month/day) (q=quit):
*/
// \\\\d{4},四位数的年份
// (?:0?[1-9]|1[0-2]),正则表达式的这一部分包括在括号中,从而确保正确的优先级。这里
//不需要使用任何捕捉组,所以使用了(?:…),内部的表达式由|字符分隔的两部分组成。
// (?:0?[1-9]|[1-2][0-9]|3[0-1]),匹配1、2,9、03、04或11、23或30、31
/<regex>/<string>/<iostream>

2 regex_match(string, smatch, regex)

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
\tregex r("(\\\\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|[1-2][0-9]|3[0-1])");
\twhile (true) {
\t\tcout << "Enter a date (year/month/day) (q=quit): ";
\t\tstring str;
\t\tif (!getline(cin, str) || str == "q")
\t\t\tbreak;
\t\tsmatch m; // 捕捉组来提取子字符串
\t\tif (regex_match(str, m, r)) {
\t\t\tint year = stoi(m[1]);
\t\t\tint month = stoi(m[2]);
\t\t\tint day = stoi(m[3]);
\t\t\tcout << " Valid date: Year=" << year << ", month=" << month << ", day=" << day << endl;
\t\t} else {
\t\t\tcout << " Invalid date!" << endl;
\t\t}
\t}
\treturn 0;
}
/*
Enter a date (year/month/day) (q=quit): 2012/11/11
Valid date: Year=2012, month=11, day=11
Enter a date (year/month/day) (q=quit):
*/
/<regex>/<string>/<iostream>

3 regex_search(string, smatch, regex)

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
\tregex r("//\\\\s*(.+)$");
\twhile (true) {
\t\tcout << "Enter a string with optional code comments (q=quit): ";
\t\tstring str;
\t\tif (!getline(cin, str) || str == "q")
\t\t\tbreak;
\t\tsmatch m;
\t\tif (regex_search(str, m, r))
\t\t\tcout << " Found comment '" << m[1] << "'" << endl;
\t\telse
\t\t\tcout << " No comment found!" << endl;
\t}
\treturn 0;
}
/*Enter a string with optional code comments (q=quit): std::string str; //Our source string
Found comment 'Our source string'
Enter a string with optional code comments (q=quit):
*/
/<regex>/<string>/<iostream>

4 sregex_iterator

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
\tregex reg("[\\\\w]+");
\twhile (true) {
\t\tcout << "Enter a string to split (q=quit): ";
\t\tstring str;
\t\tif (!getline(cin, str) || str == "q")
\t\t\tbreak;
\t\t
\t\tconst sregex_iterator end;
\t\tfor (sregex_iterator iter(cbegin(str), cend(str), reg); iter != end; ++iter) {
\t\t\tcout << "\\"" << (*iter)[0] << "\\"" << endl;
\t\t}
\t}
\treturn 0;
}
/*
Enter a string to split (q=quit): This is a test
"This"
"is"
"a"
"test"
Enter a string to split (q=quit):
*/
/<regex>/<string>/<iostream>

5 regex_replace(string, regex, string)

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
\tconst string str("

Header

Some text

");
\tregex r("

(.*)

(.*)

");
\tconst string format("H1=$1 and P=$2");
\tstring result = regex_replace(str, r, format);
\t
\tcout << "Original string: '" << str << "'" << endl;
\tcout << "New string : '" << result << "'" << endl;
\treturn 0;
}
/*
Original string: '

Header

Some text

'
New string : 'H1=Header and P=Some text'
*/
/<regex>/<string>/<iostream>

-End-