Regular Expression Character Classes - C++ Regular Expression

C++ examples for Regular Expression:Match

Introduction

Character class Matches
\dany decimal digit
\D any non-digit
\wany word character
\W any non-word character
\sany whitespace character
\S any non-whitespace character

The following code tries to match birthdays to a regular expression.

Demo Code

#include <iostream> 
#include <string> 
#include <regex> 

using namespace std;

int main()/* www.j a v a2s . c  o  m*/
{
  regex expression("J.*\\d[0-35-9]-\\d\\d-\\d\\d");

  string string1 = "Jane's Birthday is 05-12-75\n"
    "Jave's Birthday is 11-04-68\n"
    "John's Birthday is 04-28-73\n"
    "Joe's Birthday is 12-17-77";

  smatch match;

  while (regex_search(string1, match, expression, regex_constants::match_not_eol))
  {
    cout << match.str() << endl; // print the matching string 

                   // remove the matched substring from the string 
    string1 = match.suffix();
  }
}

Result


Related Tutorials