vc用正則表達

來源:互聯網
上載者:User
本文摘要翻譯了幾篇文章的內容,簡單介紹 ATL CAtlRegExp,GRETA,Boost::regex 等Regex庫,這些運算式庫使我們可以方便地利用正則庫的巨大威力,給我們的工作提供了便利。

  Regex文法

字元元 意義
. 匹配單個字元
[ ] 指定一個字元類,匹配方括弧內的任一字元。例:[abc] 匹配 "a", "b"或 "c"。
^ 如果^出現在字元類的開始處,它否定了字元類,這個被否定的字元類匹配除卻方括弧內的字元的字元。如:[^abc]匹配除了"a", "b"和"c"之外的字元。如果^出現在Regex前邊,它匹配輸入的開頭,例:^[abc]匹配以"a", "b"或"c"開頭的輸入。
- 在字元類中,指定一個字元的範圍。例如:[0-9]匹配"0"到"9"的數字。
? 指明?前的運算式是可選的,它可以匹配一次或不進行匹配。例如: [0-9][0-9]? 匹配"2"或"12"。
+ 指明?前的運算式匹配一次或多次。例如:[0-9]+匹配"1", "13", "666"等。

  *

指明*前的運算式匹配零次或多次。
??, +?, *? ?, +和*的非貪婪匹配版本,它們儘可能匹配較少的字元;而?, +和*則是貪婪版本,儘可能匹配較多的字元。例如:輸入"<abc><def>", 則<.*?> 匹配"<abc>",而<.*>匹配"<abc><def>"。
( ) 分組操作符。例如:(d+,)*d+匹配一串由逗號分開的數字,例如: "1"或"1,23,456"。
轉 義字元,轉義緊跟的字元。例如,[0-9]+ 匹配一個或多個數字,而 [0-9]+ 匹配一個數字後跟隨一個加號的情況。反斜線也用於表示縮寫,a 就表示任何數字、字母。如果後緊跟一個數字n,則它匹配第n個匹配群組(從0開始),例如,<{.*?}>.*?</>匹配 "<head>Contents</head>"。注意,在C++字串中,反斜線需要用雙反斜線來表示: "+", "a", "<{.*?}>.*?</ >"。
$ 放在Regex的最後,它匹配輸入的末端。例如:[0-9]$匹配輸入的最後一個數字。
| 間隔符,分隔兩個運算式,以正確匹配其中一個,例如:T|the匹配"The" 或"the"。

  

  縮寫匹配

縮寫 匹配
a 字母、數字([a-zA-Z0-9])
空格(blank): ([ ])
c 字母([a-zA-Z])
d 十進位數 ([0-9])
h 十六進位數([0-9a-fA-F])
換行: ( |( ? ))
q 引用字串("[^"]*")|(''''[^'''']*'''')
w 一段文字 ([a-zA-Z]+)
z 一個整數([0-9]+)

  ATL CATLRegExp

  ATL Server常常需要對地址、命令等複雜文字欄位資訊解碼,而Regex是強大的文字解析工具,所以,ATL提供了Regex解釋工具。

  樣本:

#include "stdafx.h"
#include <atlrx.h>
int main(int argc, char* argv[])
{
CAtlRegExp<> reUrl;
// five match groups: scheme, authority, path, query, fragment
REParseError status = reUrl.Parse(
"({[^:/?#]+}:)?(//{[^/?#]*})?{[^?#]*}(?{[^#]*})?(#{.*})?" );
if (REPARSE_ERROR_OK != status)
{
// Unexpected error.
return 0;
}
CAtlREMatchContext<> mcUrl;
if (!reUrl.Match(
"http://search.microsoft.com/us/Search.asp?qu=atl&boolean=ALL#results",
&mcUrl))
{
// Unexpected error.
return 0;
}
for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups;
++nGroupIndex)
{
const CAtlREMatchContext<>::RECHAR* szStart = 0;
const CAtlREMatchContext<>::RECHAR* szEnd = 0;
mcUrl.GetMatch(nGroupIndex, &szStart, &szEnd);
ptrdiff_t nLength = szEnd - szStart;
printf("%d: "%.*s"
", nGroupIndex, nLength, szStart);
}
}   
輸出:0: "http"
1: "search.microsoft.com"
2: "/us/Search.asp"
3: "qu=atl&boolean=ALL"
4: "results"

   Match的結果通過第二個參數pContext所指向的CAtlREMatchContext類來返回,Match的結果及其相關資訊都被存放在 CAtlREMatchContext類中,只要訪問CAtlREMatchContext的方法和成員就可以得到匹配的結果。 CAtlREMatchContext通過m_uNumGroups成員以及GetMatch()方法向調用者提供匹配的結果資訊。 m_uNumGroups代表匹配上的Group有多少組,GetMatch()則根據傳遞給它的Group的Index值,返回匹配上的字串的 pStart和pEnd指標,調用者有了這兩個指標,自然可以很方便的得到匹配結果。

  更多內容請參閱: CAtlRegExp Class

  GRETA

  GRETA是微軟研究院推出的一個Regex模板類庫,GRETA 包含的 C++ 對象和函數,使字串的模式比對和替換變得很容易,它們是:

  • " rpattern: 搜尋的模式
  • " match_results/subst_results: 放置匹配、替換結果的容器

       為了執行搜尋和替換的操作,使用者首先需要用一個描述匹配規則的字串來顯式初始化一個rpattern對象,然後把需要匹配的字串作為參數,調用 rpattern的函數,比如match()或者substitute(),就可以得到匹配後的結果。如果match()/substitute()調用 失敗,函數返回false,如果調用成功,函數返回true,此時,match_resultsObject Storage Service了匹配結果。請看例子代碼:

    #include <iostream>
    #include <string>
    #include "regexpr2.h"
    using namespace std;
    using namespace regex;
    int main() {
    match_results results;
    string str( "The book cost $12.34" );
    rpattern pat( "$(d+)(.(dd))?" ); 
    // Match a dollar sign followed by one or more digits,
    // optionally followed by a period and two more digits.
    // The double-escapes are necessary to satisfy the compiler.
    match_results::backref_type br = pat.match( str, results );
    if( br.matched ) {
    cout << "match success!" << endl;
    cout << "price: " << br << endl;
    } else {
    cout << "match failed!" << endl;
    }
    return 0;
    }   
    程式輸出將是:match success!
    price: $12.34

      您可以閱讀GRETA文檔,獲知rpattern對象的細節內容,並掌握如何自訂搜尋策略來得到更好的效率。

       注意:所有在標頭檔regexpr2.h裡的聲明都在名稱空間regex之中,使用者使用其中的對象和函數時,必須加上首碼"regex::",或者預先 "using namespace regex;" 一下,為了簡單起見,下文的範例程式碼中將省略"regex::" 首碼。 作者產生了greta.lib和regexpr2.h檔案,只需這兩個檔案的支援即可使用greta來解析Regex。

      匹配速度小議

       不同的Regex匹配引擎擅長於不同匹配模式。作為一個基準,當用模式:"^([0-9]+)(-| |$)(.*)$" 匹配字串"100- this is a line of ftp response which contains a message string"時,GRETA的匹配速度比boost(http://www.boost.org)Regex庫大約快7倍,比ATL7的 CATLRegExp快10倍之多! Boost Regex 的說明文檔帶有一個很多模式的匹配測試Performance結果。比較這個結果後,我發現GRETA在大部分情況下和Boost Regex效能差不多,但是在用Visual Studio.Net 2003編譯的情況下,GRETA還略勝一籌。

      Boost.Regex

      Boost提供了boost::basic_regex來支援Regex。boost::basic_regex的設計非常類似std::basic_string:

    namespace boost{
    template <class charT,
    class traits = regex_traits<charT>,
    class Allocator = std::allocator<charT> > class basic_regex;
    typedef basic_regex<char> regex;
    typedef basic_regex<wchar_t> wregex;
    }   
       Boost Regex 庫附帶的文檔非常豐富,樣本更是精彩,比如有兩個例子程式,不多的代碼,程式就可以直接對 C++ 檔案進行文法高亮標記,產生相應的 HTML (converts a C++ file to syntax highlighted HTML)。下面的例子可以分割一個字串到一串標記符號(split a string into tokens)。#include <list>
    #include <boost/regex.hpp>
    unsigned tokenise(std::list<std::string>& l, std::string& s)
    {
    return boost::regex_split(std::back_inserter(l), s);
    }
    #include <iostream>
    using namespace std;
    #if defined(BOOST_MSVC) || (defined(__BORLANDC__) && (__BORLANDC__ == 0x550))
    // problem with std::getline under MSVC6sp3
    istream& getline(istream& is, std::string& s)
    {
    s.erase();
    char c = is.get();
    while(c != ''''
    '''')
    {
    s.append(1, c);
    c = is.get();
    }
    return is;
    }
    #endif
    int main(int argc)
    {
    string s;
    list<string> l;
    do{
    if(argc == 1)
    {
    cout << "Enter text to split (or "quit" to exit): ";
    getline(cin, s);
    if(s == "quit") break;
    }
    else
    s = "This is a string of tokens";
    unsigned result = tokenise(l, s);
    cout << result << " tokens found" << endl;
    cout << "The remaining text is: "" << s << """ << endl;
    while(l.size())
    {
    s = *(l.begin());
    l.pop_front();
    cout << s << endl;
    }
    }while(argc == 1);
    return 0;
    }

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.