Example of C ++ TR1 Regular Expression Library

Source: Internet
Author: User

Example of C ++ TR1 Regular Expression Library
Author: que rongwen
Time: 2011/8/13

[Background]
To use regular expressions in a program, you must first have database support. currently, the main influential C ++ regular Library is the GNU Regex Library, which is part of glibc. the other name is Perl Compatible Regular Expressions. from the name, we can see that the PCRE Library is a regular expression library compatible with regular expressions in Perl. PCRE is a free open source library, it is implemented by C language, here is its official homepage: http://www.pcre.org/PCRE ++ is a C ++ package for PCRE Library. post an article about the introduction of http://hi.baidu.com/joec3/blog/item/5375d3da14ab07d6b7fd487b.html
Of course, we know that TR1 also contains a regular library, from Boost's regex. since the standard library has corresponding functions, it is natural to give priority to the functions provided by the standard library (TR1 is not a standard library yet, but sooner or later ).
Many compilers have already fully implemented the TR1 library. Here, I would like to praise Microsoft for its efforts and progress in the C ++ compiler. VC ++ 2008 has already fully implemented the TR1 database, and VC ++ 2010 can even combine the tr1 namespace into std. The urgency is evident, unfortunately, C ++ 0x is far away. of course, the gnu c ++ compiler, which has always followed, fully supports the tr1 library. http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.tr1
It should be said that the external conditions for using the tr1 database are completely mature.

[Sample Code]
Create a Win32 console project in VC ++ 2010 and select UNICODE support.

# Include <iostream> # include <string> # include <regex> int _ tmain (int argc, _ TCHAR * argv []) {std: locale loc (""); std: wcout. imbue (loc); std: wstring text (_ T ("My IP address is 109.168.0.1. "); std: wstring newIP (_ T (" 127.0.0.1 "); std: wstring regString (_ T (" (\ d + )\\. (\ d + )\\. (\ d + )\\. (\ d +) "); // expression option-ignore case-insensitive std: regex_constants: syntax_option_type fl = std: regex_constants: icase; // compile a regular expression statement std: wregex regExpress (regString, fl); // Save the search result std: wsmatch MS; // determine whether full row match if (std:: regex_match (text, MS, regExpress) {std: wcout <_ T ("regular expression:") <regString <_ T ("Match :") <text <_ T ("successful. ") <std: endl;} else {std: wcout <_ T (" regular expression: ") <regString <_ T (" Match :") <text <_ T ("failed. ") <std: endl;} // find if (std: regex_search (text, MS, regExpress) {std :: wcout <_ T ("regular expression:") <regString <_ T ("Search:") <text <_ T ("successful. ") <std: endl; for (size_t I = 0; I <ms. size (); ++ I) {std: wcout <_ T ("th") <I <_ T ("Result :\"") <ms. str (I) <_ T ("\"-"); std: wcout <_ T (" start position: ") <ms. position (I) <_ T ("length") <ms. length (I) <std: endl;} std: wcout <std: endl; // replace 1 text = text. replace (MS [0]. first, MS [0]. second, newIP); std: wcout <_ T ("text after 1 is replaced:") <text <std: endl;} else {std :: wcout <_ T ("regular expression:") <regString <_ T ("Search:") <text <_ T ("failed. ") <std: endl;} // replace 2 newIP = _ T (" 255.255.0.0 "); std: wstring newText = std: regex_replace (text, regExpress, newIP); std: wcout <_ T ("text after replacement 2:") <newText <std: endl; // end std :: wcout <_ T ("press enter to end... "); std: wcin. get (); return 0 ;}

[Code description]
1. Create a regular expression object. The following three methods are available:
(1) Use constructors
Std: regex_constants: syntax_option_type fl = std: regex_constants: icase; // syntax options. You can set the regular expression syntax of the style to be used.
Std: wregex regExpress (regString, fl );
(2) The assignment operator is used, but syntax options cannot be specified and inefficient.
Std: wregex regExpress;
RegExpress = regString;
(3) Use the assign Method.
Std: wregex regExpress;
RegExpress. assign (regString, fl );
The so-called "Compilation" is used to construct a regular object ".

2. regex_match () and regex_search ()
Regex_match () returns true only when the entire string matches the regular expression, and regex_search () returns true if the substring matches.

3. Matched result object std: wsmatch.
Anyone familiar with Perl Regular Expressions knows that after successful matching, you can use $1 $2... $ N is used to obtain the substring. The tr1 regex database stores the matching result in a std: wsmatch (UNICODE)/std: smatch (ANSI) object.
Std: wsmatch is an array composed of several std: wssub_match objects. std: wssub_match is derived from pair.
The starting position pointer of the substring is saved by std: wssub_match: first (in fact, the iterator is more accurate ).
Std: wssub_match: second stores the end position of the substring + 1 pointer (General STL principle, semi-open interval ).
Therefore, [std: wssub_match: first, std: wssub_match: second) indicates all the content of the substring.
Of course, std: wsmatch (pre-defined class of the match_result template) provides some simple methods for accessing substrings:
(1) The str (idx) method returns the std: string/std: wstring object of the corresponding substring, which is only the most common.
(2) The position (idx) method returns the starting offset of the corresponding substring. (It is not a pointer, It is the offset relative to the first byte address or begin ).
(3) length (idx) returns the length of the substring.

4. Replace the substring.
Previously speaking, std: wssub_match: first/second stores the start/end position of the substring, so we can use this pointer (iterator) to replace the text (see "replace 1" in the Code ").
You can also use std: regex_replace () (see "replace 2" in the Code ").

For more information about database TR1, see <C ++ authoritative guide to database expansion> by Peter Becker, translated by Shi Xiaoming and published by the Mechanical Industry Press.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.