ATL regular expression library

zhaozj2021-02-16  45

ATL regular expression library

ATL, due to the needs of ATL Server, complex text paragraphs, commands, etc.

The resolution decoding, and the regular expression is a recognized strongest text resolution tool, so ATL provides some useful tables

The Daren library is convenient for our work.

1, CATLREGEXP class

statement:

Template

Class CATLREGEXP;

initialization:

Unlike Microsoft's Greta class (another regular expression class library launched by Microsoft Research Institute), CATLREGEXP is not

There is a method of providing an initial matching string in a constructor, but allows the user to call it by calling it () method.

Use the regular expression string as a parameter, you can construct a class we need to match, for example

Need to match a time format, can be H: mm, or HH: mm, then we can construct our this way

CATLREGEXP class:

CATLREGEXP <> RE;

Re.Parse ("{[0-9]? [0-9]}: {[0-9] [0-9]}");

The regular expression syntax of ATL and the regular expression syntax of Perl is similar, but there is a place worth noting.

It is ATL in braces ({}) to represent the group in the string, and the expression above we declare 2

A group, one is [0-9]? [0-9], the other is [0-9] [0-9].

match:

Call the match () method of CATLREGEXP, you can use this class to match, the prototype of the Match method is as follows:

Bool Match (Const Rechar * Szin, CATLRematchContext * PContext, Const

Rechar ** ppszend = null)

The meaning of the parameters is obvious, but it should be noted that the first parameter is: const rechar * szin is a

Const pointer, which indicates that we can easily use the C_STR () method of the Std :: String class to deliver parameters.

Match's results return to the CATLRematchContext <> category pointed to by the second parameter PCONText.

The results and their related information are stored in the CATLRematchContext class, we only need to access

The method and members of CATLRematchContext can get the results.

2, CATLRematchContext class

statement:

Template

Class CatlremeatchContext

use:

CATLRematchContext provides matching knots to callers through M_unumGroups members and getMatch () methods

Fruit information. MaxumGroups represents how many groups on the match, getMatch (), based on the group passed to it.

The index value, returns the PStart and PEND pointers of the string on the match, the caller has these two pointers, naturally

It is very convenient to get a matching result.

3, a small example

The following example comes from MSDN, demonstrating a typical method of using CatLRegexp and CATLRematchContext classes:

#include "stdafx.h" #include

Int main (int Argc, char * argv [])

{

CATLREGEXP <> REURL;

// Five Match Groups: Scheme, Authority, Path, Query, Fragment

ReparseError Status = REURL.PARSE

"({[^: /? #] }:)? (/ {[^ /? #] *})? {[^? #] *} (? {[^ #] *})? {. *})? ");

IF (repartment_error_ok! = status)

{

// Unexpected error.

Return 0;

}

CATLRematchContext <> mcurl;

IF (! REURL.MATCH)

"http://search.microsoft.com/us/search.asp?qu=atlbs",

& mcurl))

{

// Unexpected error.

Return 0;

}

For (uint ngroupIndex = 0; ngroupIndex

ngroupIndex)

{

Const CatlrematchContext <> :: rechar * szstart = 0;

Const CatlremeatchContext <> :: rechar * szend = 0;

McURL.GETMATCH (NgroupIndex, & Szstart, & Sze);

PTRDIFF_T NLENGTH = SZEND - SZSTART;

Printf ("% d: /"%.*s/"/n", ngroupIndex, NLENGTH, SZSTART);

}

}

OUTPUT

0: "http"

1: "Search.Microsoft.com"

2: "/us/search.asp"

3: "Qu = ATL & Boolean = all"

4: "Results"

The regular expression used in the example is:

({[^: /? #] }:)? (// {[^ /? #] *})? {[^? #] *} (? {[^ #] *})? (# { *})? *})? *})?

With () as the boundary mark, a total of 5 groups, the first group is {[^: /? #] }:, ^ Is "non-" the meaning of "not", then, that is, the first group starts from the beginning, always To:, /,?, # It ends. Contact The resulting string to be matched is HTTP.

4. Customize the abbreviation form of a string

For convenience, ATL has helped us define some simple forms of regular expressions that are often used. For example: / d

([0-9]), / n standby (/ r | (/ r? / N)), etc.. These abbreviations are reflected in CatlRechartraitsa /

In CATLRECHARTRAITSW, these classes are passed to CATLREGEXP as a template parameter.

CATLRematchContext, we can define your own matching string abbreviated.

Class Catlrechartraitsa

{

Static const rechartype ** getabbrevs ()

{

Static const rechartype * s_szabbrevs [] =

{

"A ([A-ZA-Z0-9]", // alpha numeric "B ([// t])", // White Space (Blank)

"C ([A-ZA-Z])", // alpha

"D ([0-9])", // DIGIT

"h ([0-9A-FA-F])", // HEX DIGIT

"N (/ r | (/ r? / n)", // newline

"Q (/" [^ / "] * /") | (/ '[^ /'] * / ') ", // quoted string

"W ([A-ZA-Z] )", // Simple Word

"z ([0-9] )", // integer

NULL

}

Return s_szabbrevs;

}

}

The above is the code extracted by AtlRx.h, you can clearly see that the ATL is through a getabband () function.

To define string abbreviations. To define new abbreviations, we only need this:

Class MyRegtraits: Public Atl :: CatlRechartraitsa

{

PUBLIC:

Static const rechartype ** getabbrevs ()

{

Static const rechartype * s_szabbrevs [] =

{

"A ([A-ZA-Z0-9])", // alpha numeric

"B ([// t])", // White Space (Blank)

"C ([A-ZA-Z])", // alpha

"D ([0-9])", // DIGIT

"h ([0-9A-FA-F])", // HEX DIGIT

"N (/ r | (/ r? / n)", // newline

"Q (/" [^ / "] * /") | (/ '[^ /'] * / ') ", // quoted string

"W ([A-ZA-Z] )", // Simple Word

"z ([0-9] )", // integer

"e ([0-8] )", // Add yourself

NULL

}

Return s_szabbrevs;

}

}

Let our own defined Trait class inheritance from CATLRECHARTRAITSA, then rewrite the getabbrevs () function, increase

Some shorthand needs can be used. The following code example shows the "/ e" simply defined in our own class.

Slightly expressed:

int main ()

{

Atl :: CATLREGEXP Re;

Re.Parse ("// e ");

Atl :: CATLRematchContext MC;

Bool res1 = Re.match ("678", & mc); // Returns True: Successful Match

Res1 = Re.match ("999", & mc); // Returns False: Match Fail

}

Just construct ATL :: CatLRegexp and Atl :: CATLRematchContext class, transfer to our

The MyRegtraits class can use the briefed symbol you defined directly as the parameters of Traits. 5, end

Although now there is a very famous regular expression library such as Boost :: Regex, Greta in the C community, but

As a template library that comes with VC , the regular expression library in ATL still provides great convenience to our work. due to

ATL is the official release of Microsoft, so it has a good document description, strict testing and Microsoft's official

Technical Support. In addition, when developing COM components with ATL, it is more convenient to use the huge power of the regular library.

Due to the limitations of my knowledge, the content of the article is inevitable, if you have a criticist, please mail: firingme@sina.com

转载请注明原文地址:https://www.9cbs.com/read-26106.html

New Post(0)