tags:

views:

712

answers:

4

There is a simple C++ method to use pattern matching on strings? The code should sound like this:

if (regexpcmp("l?nole*[0-9]", "linoleum1")) {
  //we have a match!
} else {
   //no match 
}
+10  A: 

Did you already look at Boost.Regex?

const boost::regex e("l?nole*[0-9]");
if (regex_match("linoleum1", e)) {
  //we have a match!
} else {
  //no match 
}
Pukku
+3  A: 

Not in the core language. Use Boost.Regex or an external library like pcre. In a unix environment you almost certainly have access to the BSD regular expression tools (regcomp, regerror, regexec, regfree) which are c-like rather than c++-like but do work.

dmckee
I'm in *nix enviroinment so i'll use the BSD regular expression tools like the example at the bottom of this pagehttp://www.opengroup.org/onlinepubs/000095399/functions/regcomp.html
Emilio
+3  A: 

Take boost.regex friend. if you are not allowed to use boost (sadly, there are still companies doing this), you could look into pcrecpp, which is a C++ binding developed by google for the famous PCRE library.

Johannes Schaub - litb
lol. If you're not allowed to use a 3rd party library, then you just use a 3rd party library :-) Such companies are stupid.
gbjbaanb
What, like Google for instance? Its published coding standard allows use of only a few specific things from Boost. They must be stupid like a multi-billionaire fox ;-)
Steve Jessop
A: 

A standard regex library (that is based on boost::regex) is available in the TR1 namespace if you use lasts versions of the most used compilers : std::tr1::regex.

Klaim