Show toolbar

2013年7月8日 星期一

Using Regular expression in C/C++

標題:C/C++使用正規表示法
VC++ (main.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "stdafx.h"
#include <iostream>
#include <regex> //string lib here
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//String Match
string strA = "1337";
regex regA("[0-9]+");
if(regex_match(strA, regA))
{
cout << "A Match." << endl;
} else {
cout << "A Not Match." << endl;
}
//String Search
string strB = "cad23342";
regex regB("[0-9]");
if(regex_search(strB, regB))
{
cout << "B Match." << endl;
} else {
cout << "B Not Match." << endl;
}
//String Replace
string strC = "Hello I'm QQBoxy!!";
regex regC(" ");
string wordC = "\n";
cout << regex_replace(strC, regC, wordC) << endl;
//String Replace
string strD = "Hello I'm QQBoxy!!";
regex regD("a|e|i|o|u");
string wordD = "[$&]";
cout << regex_replace(strD, regD, wordD) << endl;
return 0;
}
說明:
在C/C++中正規表示式常用的三種語法。

沒有留言:

張貼留言