Separating C + + string strings with regular expressions

Source: Internet
Author: User

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

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.