简体   繁体   中英

Regex error with boost

I'm trying to match a string who looks like:

/new-contact?id=nb&name=test or /new-contact?id=nb

Basically the number of arguments is undefined.

so I have tried this regular expression:

boost::regex re("^/new-contact\\?(([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+$");

but when I try to use re with the following function:

function test()
{
    std::string input("/new-contact?id=5&name=Test");
    boost:cmatch token;
    boost::regex_match(req.c_str(), token, input);
    std::cout << token[1] << std::endl;
}

I get

output: name=Test

and if I change the input string to

std::string input("/new-contact?id=5&");

I get

output: id=5

I guess I am only getting the last token but I am suppose to get everything with the last "+" ?

What did I miss?

It's now working with:

^/new-contact\\?((([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+)$

token[0] is going to contain the entire match. Subsequent indices give you the sub-tokens of the match, which are determined by the parenthesis in your expression (parenthesized groups are called capturing groups ; use (?:...) for non-capturing groups).

This is documented here . Copying the provided example,

#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
#include <iostream>

using namespace boost;

regex expression("([0-9]+)(\\-| |$)(.*)");

// process_ftp: 
// on success returns the ftp response code, and fills 
// msg with the ftp response message. 
int process_ftp(const char* response, std::string* msg)
{
   cmatch what;
   if(regex_match(response, what, expression))
   {
      // what[0] contains the whole string 
      // what[1] contains the response code 
      // what[2] contains the separator character 
      // what[3] contains the text message. 
      if(msg)
         msg->assign(what[3].first, what[3].second);
      return std::atoi(what[1].first);
   }
   // failure did not match 
   if(msg)
      msg->erase();
   return -1;
}

I suggest that regular expressions are the wrong tool for parsing URL paths. May I suggest an URL parsing library ?

您可以尝试使用延续转义\\G

^/new-contact\\?|(?>\\G([^=]+)=([^&]+)&?)+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM