Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/regex"

From cppreference.com
< cpp
(added "overview" example)
m (Shorten template names. Use {{lc}} where appropriate.)
Line 2: Line 2:
 
{{cpp/regex/navbar}}
 
{{cpp/regex/navbar}}
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | regex }}
+
{{dsc header | regex }}
{{dcl list end}}
+
{{dsc end}}
  
 
The regular expressions library provides a class that represents [[enwiki:Regular expression|regular expressions]], which are a kind of mini-language used to perform pattern matching within strings.
 
The regular expressions library provides a class that represents [[enwiki:Regular expression|regular expressions]], which are a kind of mini-language used to perform pattern matching within strings.
Line 14: Line 14:
 
These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters.
 
These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/regex/dcl list basic_regex }}
+
{{dsc inc | cpp/regex/dcl list basic_regex }}
{{dcl list template | cpp/regex/dcl list sub_match }}
+
{{dsc inc | cpp/regex/dcl list sub_match }}
{{dcl list template | cpp/regex/dcl list match_results }}
+
{{dsc inc | cpp/regex/dcl list match_results }}
{{dcl list end}}
+
{{dsc end}}
  
 
===Algorithms===
 
===Algorithms===
Line 24: Line 24:
 
These functions are used to apply the regular expression encapsulated in a regex to a target sequence of characters.
 
These functions are used to apply the regular expression encapsulated in a regex to a target sequence of characters.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/regex/dcl list regex_match }}
+
{{dsc inc | cpp/regex/dcl list regex_match }}
{{dcl list template | cpp/regex/dcl list regex_search }}
+
{{dsc inc | cpp/regex/dcl list regex_search }}
{{dcl list template | cpp/regex/dcl list regex_replace }}
+
{{dsc inc | cpp/regex/dcl list regex_replace }}
{{dcl list end}}
+
{{dsc end}}
  
 
===Iterators===
 
===Iterators===
Line 34: Line 34:
 
The regex iterators are used to traverse the entire set of regular expression matches found within a sequence.
 
The regex iterators are used to traverse the entire set of regular expression matches found within a sequence.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/regex/dcl list regex_iterator }}
+
{{dsc inc | cpp/regex/dcl list regex_iterator }}
{{dcl list template | cpp/regex/dcl list regex_token_iterator }}
+
{{dsc inc | cpp/regex/dcl list regex_token_iterator }}
{{dcl list end}}
+
{{dsc end}}
  
 
===Exceptions===
 
===Exceptions===
Line 43: Line 43:
 
This class defines the type of objects thrown as exceptions to report errors from the regular expressions library.
 
This class defines the type of objects thrown as exceptions to report errors from the regular expressions library.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/regex/dcl list regex_error }}
+
{{dsc inc | cpp/regex/dcl list regex_error }}
{{dcl list end}}
+
{{dsc end}}
  
 
===Traits===
 
===Traits===
Line 51: Line 51:
 
The regex traits class is used to encapsulate the localizable aspects of a regex.
 
The regex traits class is used to encapsulate the localizable aspects of a regex.
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/regex/dcl list regex_traits }}
+
{{dsc inc | cpp/regex/dcl list regex_traits }}
{{dcl list end}}
+
{{dsc end}}
  
 
===Constants===
 
===Constants===
  
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list namespace | std::regex_constants }}
+
{{dsc namespace | std::regex_constants }}
{{dcl list template | cpp/regex/dcl list syntax_option_type}}
+
{{dsc inc | cpp/regex/dcl list syntax_option_type}}
{{dcl list template | cpp/regex/dcl list match_flag_type}}
+
{{dsc inc | cpp/regex/dcl list match_flag_type}}
{{dcl list template | cpp/regex/dcl list error_type}}
+
{{dsc inc | cpp/regex/dcl list error_type}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Example===
 
===Example===

Revision as of 19:51, 31 May 2013

Defined in header <regex>

The regular expressions library provides a class that represents regular expressions, which are a kind of mini-language used to perform pattern matching within strings.

Also provided in the regular expressions library are utility classes that provide support for various algorithms, iterators, exceptions, and type traits.

Contents

Main classes

These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters.

Template:cpp/regex/dcl list basic regexTemplate:cpp/regex/dcl list sub matchTemplate:cpp/regex/dcl list match results

Algorithms

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

Template:cpp/regex/dcl list regex matchTemplate:cpp/regex/dcl list regex searchTemplate:cpp/regex/dcl list regex replace

Iterators

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

Template:cpp/regex/dcl list regex iteratorTemplate:cpp/regex/dcl list regex token iterator

Exceptions

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

Template:cpp/regex/dcl list regex error

Traits

The regex traits class is used to encapsulate the localizable aspects of a regex.

Template:cpp/regex/dcl list regex traits

Constants

Template:cpp/regex/dcl list syntax option typeTemplate:cpp/regex/dcl list match flag typeTemplate:cpp/regex/dcl list error type
Defined in namespace std::regex_constants

Example

#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 greater 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 greater 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].