Cpp Libs Regex
The `` header file in the C++ standard library provides regular expression functionality, allowing developers to use a very flexible way to search, replace, or split strings. Regular expressions are a powerful text processing tool, widely used in data validation, text analysis, and pattern matching fields.
A regular expression is a pattern that uses a single string to describe or match a series of strings that conform to certain syntactic rules. In C++, regular expressions are implemented through the `` library.
When using regular expressions in C++11's ``, you need to escape the backslash in string literals, which means writing double backslashes .
> **Why double backslashes?**
>
>
> The C++ compiler processes string literal escape characters first, and the regular expression engine processes them later.
>
>
> For example, if you write d+, the compiler will treat d as an unknown escape character and report an error or convert it to d. The correct way is to write d+, where the compiler first converts to , and then the regex engine reads d.
> C++11 also supports raw strings, which can avoid double backslashes and are very suitable for writing complex regex: regex re(R"(d+)"); // Raw string, no need to write d
> * The content inside R"(...)" will not be processed for escaping
> * Recommended when writing complex regex with multiple layers of
## Basic Syntax
### Basic Components of Regular Expressions
* **Character Class**: e.g., `` matches any one of the characters a, b, or c.
* **Quantifiers**: e.g., `*` (zero or more times), `+` (one or more times), `?` (zero or one time).
* **Boundary Matching**: e.g., `^` (start of line), `$` (end of line).
* **Grouping**: Use parentheses `()` to create a group.
### Main Classes and Functions of C++ `` Library
* `std::regex`: Represents a regular expression object.
* `std::regex_match`: Checks whether the entire string matches the regular expression.
* `std::regex_search`: Searches for parts that match the regular expression in a string.
* `std::regex_replace`: Replaces parts that match the regular expression in a string.
* `std::sregex_iterator`: Iterator for traversing all matches.
## Examples
### 1. Full Match: regex_match
## Examples
#include
#include
using namespace std;
int main(){
regex re("d+");// Match numbers
string s ="12345";
if(regex_match(s, re))
cout<<"Match successfuln";
else
cout<<"Match failedn";
}
Output:
Match successful
### 2. Check if a String Matches a Regular Expression
## Examples
#include
#include
#include
using namespace std;
int main(){
string text ="Hello, World!";
regex pattern("^+, +!$");
if(regex_match(text, pattern)){
cout<<"The string matches the pattern."<< endl;
}else{
cout<<"The string does not match the pattern."<< endl;
}
return 0;
}
Output:
The string matches the pattern.
### 3. Search for Matches in a String
## Examples
#include
#include
#include
using namespace std;
int main(){
string text ="123-456-7890 and 987-654-3210";
regex pattern("d{3}-d{3}-d{4}");
smatch matches;
while(regex_search(text, matches, pattern)){
cout<<"Found: "<< matches<< endl;
text = matches.suffix().str();// Continue searching the remaining part
}
return 0;
}
Output:
Found: 123-456-7890Found: 987-654-3210
### 4. Replace Matches in a String
## Examples
#include
#include
#include
using namespace std;
int main(){
string text ="Hello, World!";
regex pattern("World");
string replacement ="Universe";
string result = regex_replace(text, pattern, replacement);
cout<<"Original: "<< text << endl;
cout<<"Modified: "<< result << endl;
return 0;
}
Output:
Original: Hello, World!Modified: Hello, Universe!
* * *
## More Content
## Common Classes and Types
| Name | Description |
| --- | --- |
| `std::regex` | Compiled regular expression object |
| `std::smatch` | `std::string` match result |
| `std::cmatch` | C string match result (`const char*`) |
| `std::sregex_iterator` | Match iterator for `std::string` |
| `std::cregex_iterator` | Match iterator for C strings |
| `std::syntax_option_type` | Regular expression syntax options |
| `std::match_flag_type` | Match control flags |
### Common Functions
| Function Name | Purpose | Return Value | Example |
| --- | --- | --- | --- |
| `regex_match(s, re)` | Check if entire string matches | `bool` | `regex_match("123", re)` |
| `regex_search(s, m, re)` | Find first match | `bool` | `regex_search(s, m, re)` |
| `regex_replace(s, re, fmt)` | Replace matches | `string` | `regex_replace(s, re, "#")` |
### `smatch` Match Result Object
| Member | Description | Example |
| --- | --- | --- |
| `m.str()` | Returns the entire matched content | `cout << m.str()` |
| `m` | Returns the i-th capture group | `m.str()` |
| `m.position()` | Match start position | `m.position(0)` |
| `m.size()` | Number of capture groups | `m.size()` |
| `m.empty()` | Whether the match is empty | `m.empty()` |
### Common Regular Expression Syntax
| Symbol | Meaning | Example | | |
| --- | --- | --- | --- | --- |
| `.` | Any character | `a.c` matches `abc` | | |
| `d` | Digit | `d+` | | |
| `w` | Alphanumeric and underscore | `w+` | | |
| `s`
YouTip