Boost::regex_replace Custom substitution function Usage __regex

Source: Internet
Author: User

Key words
Boost Regular expression Regex_replace custom replace function

Background
vs2005/boost1.48.0

In a multi-tier (multilayer) distributed system, the persistence layer and the business logic layer reflect the physical and logical views of the data (database) respectively. The interaction between client and server is based on the business language,
This minimizes the impact of the physical layer (background database) on the upper business and on the client side.
The client requests data to the server usually with an advanced filter (in layman's terms, where). The advanced filter is different from the original filter, and the original filter is the SQL WHERE clause, which can be
Stitching directly into a SELECT statement; the advanced filters are different, and the fields in the Advanced filter use fields from the logical model, for example:
"The Document number = ' 20021000 ' and the place of shipment like '% Beijing '".
"Document Number", "place of issue", "Purchase price" are logical view names, not real field names. As a result, the server needs to find the logical field names in the advanced filters and convert them to physical field names. The result of the replacement is: "Djbh= ' 20021000 ' and FHD like '% Beijing '"

Ideas for solving problems
Extract the logical field name from the Advanced filter with a regular expression, and then find the real field name to replace it with the logical field name lookup mapping table. Extracts a logical field name using a regular expression.
Use the custom callback function of Regex_replace to find the real field name based on the logical field Name lookup mapping table.

The sample code is as follows. Several uses of member and non-member functions as callback functions are given in the example.

Stringmatch.cpp: Defines the entry point for a console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
using namespace Std;

String Replace_callback (boost::match_results<std::string::const_iterator> match) {
if (match[0].str () = = "Document Number") {
return "DJBH";
}else if (match[0].str () = "place of issue") {
return "FHD";
}else if (match[0].str () = "Purchase Price") {
return "JHJ";
}else{
return Match[0].str ();
}
}

Class classa{
Public
String Replace_callback (boost::match_results<std::string::const_iterator> match) {
if (match[0].str () = = "Document Number") {
return "DJBH";
}else if (match[0].str () = "place of issue") {
return "FHD";
}else if (match[0].str () = "Purchase Price") {
return "JHJ";
}else{
return Match[0].str ();
}
}
};

int _tmain (int argc, _tchar* argv[])
{
std::string s = "Document number = ' 20021000 ' and the place of shipment like '%123% ' and purchase price = document Number";
Boost::regex Reg ("([^ |\\=|<|>]+)", Boost::regex::icase);///< The regular expression extracts words in the form of spaces, <,>,=, and so on. If the <,>,= and spaces appear in single quotes will also be segmented, it is obviously not the case. Welcome friends who are familiar with regular expressions give a complete expression

string ret;

< use Boost::function to encapsulate member functions as callback functions 1
ClassA A;
Boost::function<std::string (boost::match_results<std::string::const_iterator>) > function1 =
Boost::bind (&classa::replace_callback, &a, _1);
Ret=boost::regex_replace (S,reg,function1);
std::cout<<ret<<endl;

< use member functions as callback functions 2
Ret=boost::regex_replace (s,reg,std::bind1st (Std::mem_fun (&classa::replace_callback), &a));
std::cout<<ret<<endl;

< use boost::function to encapsulate normal functions as callback functions 1
Boost::function<std::string (boost::match_results<std::string::const_iterator>) > function2 =
Boost::bind (&::replace_callback,_1);
Ret=boost::regex_replace (S,reg,function2);
std::cout<<ret<<endl;

< use normal functions directly as callback functions 2
Ret=boost::regex_replace (S,reg,replace_callback);
std::cout<<ret<<endl;

return 0;
}


Resources
Http://stackoverflow.com/questions/4156559/boost-custom-formatter-using-boost-bind-to-method-taking-only-one-paramerter

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.