A small example is written using a regular expression to separate C + + string strings.
#include "stdafx.h"
#include <regex>
#include <string>
#include <vector>
#include <iostream>
using namespace Std;
#define PATTERN "[\\s]+"
Splitting a string with regular pattern
void splitstring (String str, string pattern, vector<string>& vect)
{
Regex pattern (pattern);
Const Std::sregex_token_iterator END;
for (Std::sregex_token_iterator I (Str.begin (), str.end (), pattern); I! = end; ++i) {
Std::cout << *i << Std::endl;
Vect.push_back (*i);
}
}
int main ()
{
String str = "! 1 S BODYRIGHT_UNABLATION.GRD ";
Vector<string> v;
Splitstring (str, PATTERN, v);
for (int i = 0; i < v.size (); i + +) {
cout << V[i] << Endl;
}
return 0;
}
One of the important points of knowledge comes from http://blog.sina.com.cn/s/blog_ac9fdc0b0101oow9.html
****************************************************************************************************
Regular Expression Syntax
The characteristics of regular expressions fall into many categories, and the following is a Perl-type regex.
============================================================================================
characters with special meanings
. : Any single character
[]: Character Set
{}: Count
(): Sub-mode
\: The next character has a special meaning
*: 0 or more
+: one or more
? : 0 or one
| : OR
^: The beginning of a line;
$: End of line
===========================================================================================
Character Set
\d: A decimal number
\l: A lowercase letter
\s: A whitespace character (whitespace, tab, etc.)
\u: one Capital letter
\w: One letter (a~z or a~z) or number (0~9) or underscore (_)
\d: Characters other than \d
\l: Characters other than \l
\s: Characters other than \s
\u: Characters other than \u
\w: Characters other than \w
===========================================================================================
Repeat
{n}: Strictly repeats n times
{N,}: Repeat N or more times
{n,m}: Repeat at least n times, up to M times
*: {0,}
+: {1,}
? : {0,1}
===========================================================================================
Sub-mode
To specify a sub-pattern in a pattern, enclose it in parentheses
(\d*:)? (\d+): it indicates that the first half of the string can be empty, if it is not empty, it is any length number followed by a colon, and the second half is a sequence of one or more numbers.
===========================================================================================
options available
| Represents the concept of a two election.
Subject: (fw:| Re:): Indicates a match SUBJECT:FW: or subject:re:
===========================================================================================
Regular Expression Error
When we enrich a pattern with a regex, it checks the pattern and throws a Bad_expression exception if it finds that the pattern is illegal or too complex to match.
Separating C + + strings with regular expressions string