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-


分享到:


相關文章: