The C++ standard library handles regular expression class std::regex_C language _ Script home

Detail the C++ standard library class std::regex that deals with regular expressions

Updated: March 27, 2024 at 10:21:49 by Iron Pine Rote py
std is the namespace of the C++ standard library, which contains a large number of standard C++ Classes, functions and objects, these classes and functions provide a wide range of functions, including input and output, containers, algorithms, string processing, etc. This article mainly introduces the C++ standard library provides for processing regular expressions of the class std::regex, need friends can refer to the next

std is the namespace of the C++ standard library and contains a large number of standard C++ classes, functions, and objects. These classes and functions provide a wide range of functionality, including input and output, containers, algorithms, string handling, and more.

Usually, to use objects and functions from the standard library, you include the appropriate header file in your code, such as #include . You can then use the std:: prefix, such as std::cout, std::cin, std::endl, etc.

This usage helps prevent naming conflicts, since multiple libraries in C++ may provide the same name. Using namespaces makes it clear that you want to use a feature in the standard library rather than a feature of the same name defined elsewhere.

Common classes and functions in the C++ standard library:

1. Class: - 'std::string' :string processing class. - 'std::vector' : dynamic array container class. - 'std::map', 'std::unordered_map' : key-value pair mapping container class. - 'std::fstream' : file input/output class. - 'std::deque' : double-ended queue container class. - 'std::set', 'std::unordered_set' : collection container class. - 'std::stack', 'std::queue' :stack and queue container adapter class. - 'std::stringstream' :stringstream class.

2. Functions: - 'std::cout', 'std::cerr' : console output function. - 'std::cin' : console input function. - 'std::sort' : container sorting function. - 'std::find' : container search function. - 'std::max', 'std::min' : Returns the greater or lesser of the two values. - 'std::accumulate' : container element summation function. - 'std::copy' : copies a range element to another range function. - 'std::transform' : container element conversion function. - 'std::regex_search' : regular expression search function. - 'std::regex_match' : indicates the regular expression matching function. - 'std::regex_replace' : regular expression replacement function.

3. Object: - 'std::endl' : Wrap and refresh the output stream object. - 'std::numeric_limits' : indicates the value type limit information object. - 'std::allocator' : dynamic memory allocator object. - 'std::cin.eof()' : Input stream object function to check whether the end of the file is reached. - 'std::nothrow' : An object that returns a null pointer without throwing an exception when memory allocation fails. - 'std::random_device' : truly random number generation object. - 'std::locale' : Object that controls the localization behavior of the C++ standard library.

These classes, functions, and objects provide a wealth of functionality covering input and output, containers, algorithms, string processing, regular expressions, and more, providing C++ programmers with powerful tools for various types of application development.

------------

'std::regex' is a class provided in the C++ standard library for handling regular expressions. Regular expressions are a powerful pattern matching tool, which can be used to perform complex search and replace operations in strings. The std::regex class provides a way to create, compile, and use regular expressions.

Here are some important member functions and uses of the 'std::regex' class:

1. Constructor: - `explicit regex(const char* fmt, flag_type flags = std::regex_constants::ECMAScript)` - `explicit regex(const std::string& fmt, flag_type flags = std::regex_constants::ECMAScript) 'These constructors are used to create a' std::regex 'object that takes a regular expression string as an argument and optionally specifies matching flags.

2. Member functions' match() 'and' search() ': - `bool match(const std::string& s, std::regex_constants::match_flag_type flags = std::regex_constants::match_default) const` - `bool search(const std::string& s, std::regex_constants::match_flag_type flags = std::regex_constants::match_default) const` These two member functions are used to perform a full match(' match() ') and a partial match(' search() ') in a string, respectively. They accept a string to be matched as an argument and optionally specify a matching flag.

3. Replacement function 'std::regex_replace()' : - `std::string regex_replace(InputIt first, InputIt last, const std::regex& re, const std::string& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default)`

This function is used to search for and replace parts of the range '[first, last)' that satisfy the regular expression 're'. The replacement method is specified by the parameter 'fmt'.

4. Regular expression syntax: 'std::regex' supports a variety of regular expression syntax, including ECMAScript, basic, extended and so on. You can specify the syntax to use by setting different flags. Common flags include: - 'std::regex_constants::ECMAScript' : Use ECMAScript syntax. - 'std::regex_constants::basic' : Uses basic regular expression syntax. - 'std::regex_constants::extended' : Uses extended regular expression syntax.

These are just some of the common member functions and uses of the 'std::regex' class. With the help of these functions, regular expressions can be easily searched and replaced in strings, and complex text processing functions can be realized.

#include <iostream> #include <regex> #include <string> int main() {// Raw string std::string text = "Hello, world!" ; // Define a regular expression pattern std::regex pattern("world"); if (std::regex_search(text, pattern)) {std::cout << "Found a matching pattern in the text!" << std::endl; } else {std::cout << "No matching pattern found!" << std::endl; } return 0; }

/*std::regex: indicates a regular expression object. std::smatch: A container that holds match results, which can be filled with the std::regex_match or std::regex_search functions. std::regex_match: Used to check whether the entire string matches the regular expression. std::regex_search: Searches the input string for matches with the regular expression. std::regex_replace: Used to perform regular expression replacements in strings. std::regex_iterator: Used to iterate over all substrings in a string that match the regular expression. std::regex_token_iterator: Used to iterate over substrings in a string that match regular expressions and their non-matching parts. * /

#include <iostream> #include <regex> int main() { std::string text = "Hello, my email is example@email.com and my phone number is 123-456-7890."; std::regex emailRegex("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"); std::regex phoneRegex("\\d{3}-\\d{3}-\\d{4}"); // Match email std::smatch emailMatch; if (std::regex_search(text, emailMatch, emailRegex)) { std::cout << "Email found: " << emailMatch.str() << std::endl; } // Match phone number std::smatch phoneMatch; if (std::regex_search(text, phoneMatch, phoneRegex)) { std::cout << "Phone number found: " << phoneMatch.str() << std::endl; } // Replace phone number std::string newText = std::regex_replace(text, phoneRegex, "XXX-XXX-XXXX"); std::cout << "Text with phone number replaced: " << newText << std::endl; return 0; } /*std::regex: Represents a regular expression object. std::smatch: A container that holds match results, which can be filled with the std::regex_match or std::regex_search functions. std::regex_match: Used to check whether the entire string matches the regular expression. std::regex_search: Searches the input string for matches with the regular expression. std::regex_replace: Used to perform regular expression replacements in strings. std::regex_iterator: Used to iterate over all substrings in a string that match the regular expression. std::regex_token_iterator: Used to iterate over substrings in a string that match regular expressions and their non-matching parts. * /

To this article about the C++ standard library for processing regular expression class std::regex article is introduced to this, more related to the C++ standard library regular expression std::regex content please search the script home previous articles or continue to browse the following related articles hope that you will support the script home in the future!

Related article

  • C语言中十六进制转十进制两种实现方法

    C language hexadecimal to decimal two implementation methods

    This article mainly introduces the C language hexadecimal to decimal two implementation methods of the relevant information, the need of friends can refer to the next
    2017-01-01
  • C++模拟2D牛顿力学效果的示例代码

    C++ simulation of 2D Newtonian mechanical effects of example code

    This article mainly introduces in detail how to use C++ to simulate 2D Newton mechanics effects, the example code in the article explains in detail, has a certain learning value, interested partners can understand
    2023-06-06
  • C++中fstream,ifstream及ofstream用法浅析

    Analysis of usage of fstream,ifstream and ofstream in C++

    This article mainly introduces the usage of fstream,ifstream and ofstream in C++, which is suitable for C++ beginners to learn the operation of file flow, and the friends who need it can refer to it
    2014-08-08
  • C++ 用红黑树模拟实现set、map的示例代码

    C++ uses red-black tree simulation to implement set, map example code

    The underlying structure of set and map is red-black tree, and their functions are realized by calling the interface of red-black tree. This paper mainly introduces C++ to simulate set and map with red-black tree, which has certain reference value, and you can understand it if you are interested
    2024-03-03
  • 用C++实现一个命令行进度条的示例代码

    Example code for implementing a command line progress bar in C++

    This article mainly introduces the use of C++ to achieve a command line progress bar example code, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to learn it
    2020-04-04
  • C++对Json数据的友好处理实现过程

    C++ for Json data friendly implementation process

    In the application of Ajax, the foreground will basically use JSON as a data exchange format, so the following article mainly introduces you about C++ friendly processing of Json data, the article introduces very detailed through the example code, the need of friends can refer to the next
    2022-02-02
  • 简易Dota改键外挂程序制作

    Simple Dota change key plug-in program production

    Use the global hook to make a personalized dota game key change function, you can refer to use
    2013-11-11
  • 适合初学者练习的C语言实现三子棋小游戏

    Suitable for beginners to practice C language to achieve three chess small game

    Today, this article mainly introduces you to share a suitable for beginners to use C language to write three chess games, with a simple C language to achieve the three chess games played when I was a child, the following is a man-machine battle, of course, the code of the computer opponent is artificial intellectual disability rather than artificial intelligence details please read the following article with a small series of content
    2021-10-10
  • Qt实现编写SMTP客户端的示例详解

    Qt implementation writing SMTP client example detailed

    This article mainly introduces how to write SMTP client through Qt, which can send email through SMTP (MIME with text, html, attachments, inline files, etc.). And support SSL and SMTP authentication, interested can learn
    2022-11-11
  • Qt实现字幕滚动效果的示例代码

    Qt implementation of subtitles scroll effect example code

    This article mainly introduces how Qt uses QTimer to achieve subtitle scrolling function, and can achieve their own change of text content, adaptive text size, free to adjust the speed and other functions, interested can learn
    2022-06-06

Latest comments