Learning Boost 7 - Regex 2

xiaoxiao2021-03-06  49

Learning Boost 7

Regex 2

In the last Learning Boost, we have already finished three template classes in Regex, which we have to use these classes. We use three algorithms to use regular, regex_match, regex_search, regex_replace.

Regex_match

The Regex_Match algorithm is used to test whether a string is fully matched. Let's take a look at the use of regex_match:

IF (Regex_Match (STR, M, RE))

{

...

}

Str is a string, which can be string, wstring, char *, or wchar_t *

m is Match_Results, which saves the result of the match by reference to the incoming parameters, M To match the type of Str, which can be SMATCH, WSMATCH, CMATCH, or WCMATCH to correspond to String, WString, CHAR * or WCHAR_T * STR, respectively. .

The RE is the regular expression, which is generally Regex or Wremex.

The type of Str, M, and RE is as follows:

m Type Type Type re str string smatch (match_results ) regex (basic_regex ) wstring wsmatch (match_results ) wregex (basic_regex ) char * cmatch (match_results ) Regex (Basic_Regex ) wchar_t * wcmatch (match_results ) WREGEX (Basic_Regex )

The return value of the function indicates whether the string completely matches the regular expression. When TRUE is returned, M saves the result of the match; returns False, M is not defined.

Let's take a look at it when the function returns True, how is M.

m.size () == Re.mark_count ()

Remember what RE.MARK_COUNT () returned? In the previous one, RE.MARK_COUNT () returned, the "number" "number" is not explained in detail. Here I will explain it in detail.

In fact, this "group" is called sub-expression in the Boost's Regex. Sub-expression is part of the use of small brackets in regular style, and the regular style itself is a sub-expression, so Re.mark_Count () is equal to the small bracket log 1.

M.PREFIX () and M.SUFFIX ()

These two returns the SUB_MATCH type (equivalent to an iterator group). In the regex_match algorithm, these two returned sub_match are empty, their values ​​are as follows: (SUB_MATCH inherited in Pair, so there are first and second members)

M.PREFIX (). First == Str.Begin ()

M.PREFIX (). Second == Str.Begin ()

M.PREFIX (). matched == false

m.suffix (). first == str.end ()

m.suffix (). Second == str.end ()

M.Suffix (). matched == false

Because regex_match is full match, the entire string and regular matching match, the prefix and suffix are empty.

M [0]

Returns the 0th match, because the regex_match is completely matched, so

M [0] .first == Str.begin ()

M [0]. Second == str.end ()

M [0] .MATCHED == True

M [n], n

Returns the nth matching sub-expression.

m [n] .matched indicates whether the nth sub-expression exists in a string. The entire regex matches, but Sub_exp may match empty, such as "(a *)" can match.

m [n] .first and M [n]. Second represents the range of matching. If you match the empty, it is str.end ().

According to my test, the order of M [1], M [2], ..., M [N] is in accordance with the order of the regular left brace, such as regular formula ((a) BC) D. (EFG), if a string is matched (string only "abcdefg"), then

M [0] == "abcdefg" (SUB_MATCH is overloaded == operator makes it possible to compare with a string)

M [1] == "ABC"

M [2] == "a"

M [3] == "EFG"

REGEX_MATCH other usage

Regex_match (STR, RE) only tests whether it matches, does not need to match the result of Regex_match (BEG, END, R), is an iterator regex_match (beg, end, m, re) Note MATCH_RESULTS Regex_match ( Str, M, RE, FLAG) FLAG is a matching option, the default is REGEX_CONSTANTS :: match_default

Regex_search

The use of Regex_Search is basically the same as regex_match.

IF (Regex_Search (STR, M, RE))

{

...

}

Regex_search does not require Str to completely match RE, as long as one of the strings in the STR matches RE. Therefore, M.PREFIX () and M.Suffix () are not necessarily empty.

Regex_search is from left to right, and try to match long strings.

(to be continued)

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

New Post(0)