c++11特性之正则表达式

C++11中也将正则表达式纳入了新标准的一部分,不仅如此,它还支持了6种不同的正则表达式的语法,分别是:ECMASCRIPT、basic、extended、awk、grep和egrep。其中ECMASCRIPT是默认的语法,具体使用哪种语法我们可以在构造正则表达式的时候指定。

正则表达式库提供表示正则表达式,这是一种用于字符串内执行模式匹配小型的语言的类。

主要的类:
basic_regex
regular expression object

sub_match
identifies the sequence of characters matched by a sub-expression

match_results
identifies one regular expression match, including all sub-expression matches

算法:
These functions are used to apply the regular expression encapsulated in a regex to a target sequence of characters.

regex_match
attempts to match a regular expression to an entire character sequence

regex_search
attempts to match a regular expression to any part of a character sequence

regex_replace
replaces occurrences of a regular expression with formatted replacement text

迭代器:
The regex iterators are used to traverse the entire set of regular expression matches found within a sequence.

regex_iterator
iterates through all regex matches within a character sequence

regex_token_iterator
iterates through the specified sub-expressions within all regex matches in a given string or through unmatched substrings

异常:
This class defines the type of objects thrown as exceptions to report errors from the regular expressions library.

regex_error
reports errors generated by the regular expressions library

例子代码:

#include <iostream>
#include <iterator>
#include <string>
#include <regex>

int main()
{
    std::string s = "Some people, when confronted with a problem, think "
        "\"I know, I'll use regular expressions.\" "
        "Now they have two problems.";

    std::regex self_regex("REGULAR EXPRESSIONS",
            std::regex_constants::ECMAScript | std::regex_constants::icase);
    if (std::regex_search(s, self_regex)) {
        std::cout << "Text contains the phrase 'regular expressions'\n";
    }

    std::regex word_regex("(\\S+)");
    auto words_begin = 
        std::sregex_iterator(s.begin(), s.end(), word_regex);
    auto words_end = std::sregex_iterator();

    std::cout << "Found "
              << std::distance(words_begin, words_end)
              << " words\n";

    const int N = 6;
    std::cout << "Words longer than " << N << " characters:\n";
    for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
        std::smatch match = *i;
        std::string match_str = match.str();
        if (match_str.size() > N) {
            std::cout << " " << match_str << '\n';
        }
    }

    std::regex long_word_regex("(\\w{7,})");
    std::string new_s = std::regex_replace(s, long_word_regex, "[$&]");
    std::cout << new_s << '\n';
}
Output:
Text contains the phrase 'regular expressions'
Found 19 words
Words longer than 6 characters:
  people,
  confronted
  problem,
  regular
  expressions." problems. Some people, when [confronted] with a [problem], think "I know, I'll use [regular] [expressions]." Now they have two [problems].

你可能感兴趣的:(regex,C++11)