C + + boost regular expression usage

Source: Internet
Author: User
Tags regex expression regular expression

What is a regular expression? A regular expression is a pattern used to describe a certain amount of text. The regex represents regular Express.

If you don't know what a regular expression is, see this article: Regular expressions in simple language

With the basis of regular expressions, the question is how to use them. We're boost::regex to say

First look at an online classic 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.

First, even if you have a boost library, you need to compile the regex separately.

On-Line Introduction:

Boost library installation is more cumbersome, you need to compile the source files, I organized a bit, if only need to do regular expression, click the following code to knock on the line:

cmd

vcvars32.bat

cd D:\boost_1_32_0\libs\regex\build

d:

nmake -fvc6.mak

nmake -fvc6.mak install

Note, do not download down the packet is not much, after the decompression reached more than 100 m, compiled after 109M, occupy 131M, so the installation must pay attention to empty out enough space, typing NMAKE- After the Fvc6.mak wait for a long time, the screen will also appear a lot of English, can not be considered. Follow the steps to knock on the line. Compress the document in the package in detail, and refer to the document to continue.

Integration in VC6: Tools->options->directories->include files

Add: D:\boost_1_32_0

I used VS2003 to do the Run.bat.

chdir E:\Program\boost_1_34_1
bjam "-sTOOLS=vc-7_1" "-sVC71_ROOT=D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7" "--prefix=E:\Program\boost" "--builddir=E:\Program\boost_1_34_1\build" "-sBUILD=debug release <runtime-link>static/dynamic" --with-regex install
PAUSE

As for parameters, you need to refer to boost installation introduction http://blog.csdn.net/begtostudy/archive/2007/11/11/1879213.aspx

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.