I want to parse the strings, so that to check whether they have specified syntax or not.
Example:
Str = Z344-R565l t
Here my requirement is after Z
there should be a number and after that a -
and after that R
should be there followed by a number, followed by l
, followed by a space and then finally t
.
If any thing other than this it should be a error.
I have to parse many different kind of syntax like this. I would be awkward if write a function for each type of syntax required. I heard that yacc or lex can solve this problem.
Can any one please throw some light on my problem?
Use boost::regex
#include <string>
#include <boost/regex.hpp>
bool isMatch(std::string input){
boost::regex r("Z[0-9]*-R[0-9]*l t");
return boost::regex_search(input, r);
}
The other thing that you could do is supply a list of regex expressions in a file, one expression per line. Create a vector of boost::regex objects using the file input and iterate through the vector of patterns for each string you need to validate. It's not very efficient but it will work.