Beyond the C ++ standard library: An Introduction to boost-library 5.1 RegEx

Source: Internet
Author: User
Tags traits
Library 5. How does the regexregex library improve your program?
  • Providing regular expression support for C ++

  • Improved robustness of valid input

Regular Expressions are often used in text processing. For example, there are many ways to verify the validity of regular expressions. Consider an application that requires that the input only consist of numbers. Another program may require a special format, such as three digits followed by a letter followed by two digits. You may need to verify the zip code, credit card number, social insurance number, or something else. It is very easy to use regular expressions for such verification. In addition, you can use a regular expression to replace text, that is, replace some text with other text. Assume that you need to convert the word color to color in many documents. Regular Expressions provide the best way to do this, including remembering to replace colour and colour, colurs in the plural form, colourize, and so on. Another way to use regular expressions is to format the text.

Many popular programming languages, Perl is one of them, all with built-in support for regular expressions, but not in C ++. The C ++ standard is also silenced when it comes to regular expressions. Boost. regEx is a perfect and effective library. It incorporates regular expressions into C ++ programs and contains several different syntaxes used by common tools such as Perl, grep, and Emacs. It is one of the most famous Regular Expression Libraries in C ++, which is easy to use and extremely powerful.

How does RegEx apply to standard libraries?

The current C ++ standard library does not support regular expressions. This is a pity. There are so many requirements for regular expressions that sometimes users have to abandon C ++ to write programs that need to support regular expressions. Boost. RegEx fills the gaps in the standard in this regard, and it has been proposed to be added to the C ++ standard of the next version. Boost. RegEx has been accepted by the upcoming library technical report.

 

RegEx

Header file: "Boost/RegEx. HPP"

The regular expression is encapsulated into a type.Basic_regex. In the next section, we will discuss more about how regular expressions are compiled and analyzed. Here we will first take a rough look.Basic_regexAnd the three most important algorithms in this library.

namespace boost {
template <class charT,
class traits=regex_traits<charT> >
class basic_regex {
public:
explicit basic_regex(
const charT* p,
flag_type f=regex_constants::normal);

bool empty() const;

unsigned mark_count() const;

flag_type flags() const;
};

typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
}

Member Functions
explicit basic_regex (
const charT* p,
flag_type f=regex_constants::normal);

This constructor accepts a Character Sequence containing a regular expression, and a parameter is used to specify options when a regular expression is used, such as whether to ignore case sensitivity. IfPIf the Regular Expression in is invalid,Bad_expressionOrRegex_error. Note that these two exceptions are actually the same thing. At the time of writing this book, the name currently used has not been changed.Bad_expressionBut the boost. RegEx of the next version will useRegex_error.

bool empty() const; 

This member function is a predicate whenBasic_regexRealIn this example, if no valid regular expression existsTrueThat is, when it is assigned an empty character sequence.

unsigned mark_count() const; 

Mark_countReturnRegExThe number of marked subexpressions. A marked subexpression is a part enclosed in parentheses in a regular expression. The text matching this subexpression can be obtained by calling a regular expression algorithm.

flag_type flags() const;

Returns a bitmask containingBasic_regexThe flag of the selected option. For example, flagIcaseIndicates that the regular expression is case-insensitive.Javascript, Indicating that RegEx uses the Javascript syntax.

typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;

Do not use typeBasic_regexTo define variables, you should use these twoTypedef. These two types,RegExAndWregexIs the abbreviation of two character types, as shown in figureStringAndWstringYesBasic_string <char>AndBasic_string <wchar_t>. This similarity is different. To some extent,RegExIs a container of a specific type of string.

Common functions
template <class charT,class Allocator,class traits >
bool regex_match(
const charT* str,
match_results<const charT*,Allocator>& m,
const basic_regex<charT,traits >& e,
match_flag_type flags = match_default);

Regex_matchJudge a regular expression (ParameterE) Match the entire Character SequenceStrIt is mainly used to verify text. Note: This regular expression must match all strings to be analyzed; otherwise, the function returnsFalse. If the entire sequence is successfully matched,Regex_matchReturnTrue.

template <class charT,class Allocator, class traits> 
bool regex_search(
const charT* str,
match_results<const charT*,Allocator>& m,
const basic_regex<charT,traits >& e,
match_flag_type flags = match_default);

Regex_searchSimilarRegex_matchBut it does not require full matching of the entire character sequence. You can useRegex_searchTo find a sub-sequence in the input. The sub-sequence matches the regular expression.E.

template <class traits,class charT>
basic_string<charT> regex_replace(
const basic_string<charT>& s,
const basic_regex<charT,traits >& e,
const basic_string<charT>& fmt,
match_flag_type flags = match_default);

Regex_replaceSearch for regular expressions in the entire Character SequenceE. After each successful matchFMTFormat the matched string. By default, unmatched text is not modified, that is, the text is output but not changed.

These three algorithms have several different overload forms: one acceptsConst chart *(ChartIs a character type), the other acceptsConst basic_string <chart> &, There is also an overload that accepts two-way iterators as input parameters.

 

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.