Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE2 (PHP >=7.3)

/
^([ -~]{1,})$
/
gm

Description

match all printable basic ASCII characters in c++

#include <iostream>
#include <sys/types.h>
#include <regex.h>

using namespace std;

#define STR  "QWERTYUIOPASDFGHJKLZXCVBNM"
//#define STR    "qwertyuiopasdfghjklzxcvbnm123456789"
//#define STR "`~!@#$%^&*()-_+={[}]|\\;:i\"'<,>.?/"
#define PATTERN "^([ -~]{8,})$" //匹配所有的可以输入的ascii码

int main()
{
	cout << ' ' << int(' ') << endl;
	cout << '~' << int('~') << endl;
	cout << STR <<endl;
	cout << PATTERN  << endl;
	regex_t regex;
  if(regcomp(&regex, PATTERN, REG_EXTENDED) == 0) {
    if(regexec(&regex, STR, 0, NULL, 0) == 0) {
		cout << "match reg-exp" << endl;
    } else {
			cout << "doesn't match reg-exp" << endl;
    }
  } else {
	cout << "reg-exp compilation error" << endl;
  }
	
   return 0;
}

Submitted by zhongmeng Yu - 3 years ago