C ++ regular expressions, regex, and regex
Sometimes we use regular expressions when developing projects,
Writing Regular Expressions in PHP is simple.
But in C ++, it becomes complicated.
I want to find a small regular expression case.
Most blogs about regex written by others are particularly complex.
Finally, I got to understand MSDN.
If you have no patience with msdn, you can refer to the small case I have compiled below.
# Include "stdafx. h" # include
# Include
Using namespace std; int main () {string str = "Hello World"; regex cmd ("(EL )(. *) "); // if (regex_match (str, cmd) cout <" matched successfully "<endl; regex cmd1 ("El [a-zA-Z] +"); smatch a; // store the matched string regex_search (str, a, cmd1 ); // obtain the matched string for (auto x: a) cout <x <endl; regex cmd2 ("El [a-zA-z] + "); cout <regex_replace (str, cmd2, "World") <endl; return 0 ;}
In fact, the C ++ regular expression just needs to remember these three functions.
Regex_match (str, cmd) // returns whether the match is successful. regex_search (str, a, cmd1) // obtain the matching string regex_replace (str, cmd2, "World") // Replace the string