首页 > 科技 > C++|正则表达式的match、search、replace

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

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

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

1 regex_match(string, regex)

//通过正则表达式验证日期输入格式,如2012/11/11
#include
#include
#include
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) {
ttcout ttstring str;
ttif (!getline(cin, str) || str == "q")
tttbreak;
ttif (regex_match(str, r))
tttcout ttelse
tttcout 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

2 regex_match(string, smatch, regex)

#include 
#include
#include
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) {
ttcout ttstring str;
ttif (!getline(cin, str) || str == "q")
tttbreak;
ttsmatch m; // 捕捉组来提取子字符串
ttif (regex_match(str, m, r)) {
tttint year = stoi(m[1]);
tttint month = stoi(m[2]);
tttint day = stoi(m[3]);
tttcout tt} else {
tttcout tt}
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):
*/

3 regex_search(string, smatch, regex)

#include 
#include
#include
using namespace std;
int main()
{
tregex r("//\\s*(.+)$");
twhile (true) {
ttcout ttstring str;
ttif (!getline(cin, str) || str == "q")
tttbreak;
ttsmatch m;
ttif (regex_search(str, m, r))
tttcout ttelse
tttcout 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):
*/

4 sregex_iterator

#include 
#include
#include
using namespace std;
int main()
{
tregex reg("[\\w]+");
twhile (true) {
ttcout ttstring str;
ttif (!getline(cin, str) || str == "q")
tttbreak;
tt
ttconst sregex_iterator end;
ttfor (sregex_iterator iter(cbegin(str), cend(str), reg); iter != end; ++iter) {
tttcout tt}
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):
*/

5 regex_replace(string, regex, string)

#include 
#include
#include
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 tcout treturn 0;
}
/*
Original string: '

Header

Some text

'
New string : 'H1=Header and P=Some text'
*/

-End-

本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.sosokankan.com/article/2055663.html

setTimeout(function () { fetch('http://www.sosokankan.com/stat/article.html?articleId=' + MIP.getData('articleId')) .then(function () { }) }, 3 * 1000)