-
Introduction to Regular Expressions in C++
Regular expressions, also known nationwide as regex or regexp, are patterns used to match character combinations in strings. In C++, regular expressions can be used with the<regex>
library, which is a part of the C++ Standard Library since C++11.
To use the <regex>
library, include it in your C++ program:
1 #include <regex>
-
Using the
std::regex_match
Function
The std::regex_match
function checks if a given string matches a specified regular expression. If the string matches the regex, std::regex_match
returns true
, otherwise it returns false
.
Here’s an example of how to use std::regex_match
:
1 #include <iostream>
2 #include <regex>
3
4 int main() {
5 std::string str = "hello world";
6 std::regex pattern("hello world");
7
8 if (std::regex_match(str, pattern)) {
9 std::cout << "The string matches the regex pattern." << std::endl;
10 } else {
11 std::cout << "The string does not match the regex pattern." << std::endl;
12 }
13
14 return 0;
15}
In this example, the output will be “The string matches the regex pattern.” because the string str
matches the regex pattern pattern
.
-
Using the
std::regex_search
Function
The std::regex_search
function searches a given string for a substring that matches a specified regular expression. If a match is found, std::regex_search
returns true
, otherwise it returns false
.
Here’s an example of how to use std::regex_search
:
1 #include <iostream>
2
#include <regex>
3
4 int main() {
5 std::string str = "hello world";
6 std::regex pattern("world");
7
8 if (std::regex_search(str, pattern)) {
9 std::cout << "A substring that matches the regex pattern was found." << std::endl;
10 } else {
11 std::cout << "No substring that matches the regex pattern was found." << std::endl;
12 }
13
14 return 0;
15}
In this example, the output will be “A substring that matches the regex pattern was found.” because the string str
contains a substring that matches the regex pattern pattern
.
-
Regular Expression Syntax
The regular expression syntax used by the <regex>
library in C++ is similar to the syntax used by other programming languages and tools.
Follow the below examples of regex patterns and their meanings:
a|b
: Matches either the character ‘a’ or the character ‘b’..
: Matches any character.^
: Matches the start of the string.$
: Matches the end of the string.*
: Matches zero or more occurrences of the preceding character or pattern.+
: Matches one or more occurrences of the preceding character or pattern.?
: Matches zero or one occurrence of the preceding character or pattern.{m,n}
: Matches at least ‘m’ and at most ‘n’ occurrences of the preceding character or pattern.[a-z]
: Matches any lowercase letter.[^a-z]
: Matches any character that is not a lowercase letter.\d
: Matches any digit.\w
: Matches any word character (letters, digits, or underscores).\s
: Matches any whitespace character.
By combining these building blocks, you can create complex regular expressions to match specific patterns in strings.
About Author
Discover more from SURFCLOUD TECHNOLOGY
Subscribe to get the latest posts sent to your email.