C ++ regular expressions, regex, and regex

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.