In C + +, regular expression syntax and common regular expression encyclopedia

Source: Internet
Author: User
Tags regex expression

The formal support for regular expressions in vc++2010 is undoubtedly a highlight of the vc++2010, bringing a lot of convenience to vs. C + + developers. Through their own use, the strong function of the regular expression is deeply felt. So summarize some simple introduction, and share with everybody.
One, what is a regular expression:
A regular expression (regular expression) is a "string" that describes a feature and then verifies that another "string" conforms to this feature. is a powerful tool for verifying and manipulating strings. A simple understanding of regular expressions can be considered a special validation string. For example, the expression "ab+" describes the characteristics of "a" and any ' B ', then ' ab ', ' ABB ', ' abbbbbbbbbb ' all conform to this feature.

Second, the regular expression can be used to do what.
Regular expressions can be used to:
(1) Verify that the string conforms to the specified characteristics, such as verifying that it is a legitimate mailing address.
(2) used to find strings, finding strings that match the specified characteristics from a long text, more flexible than finding a fixed string.
(3) Used to replace, more powerful than the ordinary replacement.

three, Introduction and basic grammar:

First look at a classic online example:

#include "stdafx.h"
#include <cstdlib>

#include <stdlib.h>

#include <boost/regex.hpp>

#include <string>

#include <iostream>

using namespace Std;

using namespace boost;

Regex expression ("^select" ([a-za-z]*) from ([a-za-z]*));

int main (int argc, char* argv[])

{

Std::string in;

Cmatch what;

cout << "Enter test string" << Endl;

Getline (Cin,in);

if (Regex_match (In.c_str (), what, expression))

{

for (int i=0;i<what.size (); i++)

cout<< "str:" <<what[i].str () <<endl;

}

Else

{

cout<< "Error Input" <<endl;

}

return 0;

}

Results

Enter: Select name from table

Output: Str:select name from table

Str:name
Str:table
According to our request, the strings are matched and singled out. This is useful when dealing with a large number of rules in text format, because it is flexible and Itong.
After reading the example, you have a perceptual understanding of the regular expression. So here are some basic syntax for regular expressions, with a set of grammar rules in regular expressions, common syntax including character matching, repeat matching, character positioning, escape matching, and other advanced syntax (character grouping, character substitution, and character decision):

character matching syntax:

character Syntax

syntax explanation

Syntax Example

//d

Matching numbers (0~9)

'//d ' match 8, no match;

//d

Match non-digit

'//d ' match C, does not match 3;

//w

Match any single character

'//w//w ' match A3, does not match @3;

//w

Match non-single character

'//w ' match @, do not match C;

//s

Match white space character

'//d//s//d ' match 3 D, does not match ABC;

//s

Match non-null characters

'//s//s//s ' match a#4, mismatch 3 D;

.

Matches any character

' ... ' matches a$ 5, does not match newline;

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.