Show toolbar

2013年7月8日 星期一

Using Regular expression in C/C++

標題:C/C++使用正規表示法
VC++ (main.cpp):
#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++中正規表示式常用的三種語法。

沒有留言:

張貼留言