25 Apr
2006
25 Apr
'06
5:38 p.m.
I would like to implement matching of SQL-LIKE clauses by Boost regex. I need to convert strings like "HEL_LO%" into something regex("HEL.LO.*"). As the LIKE-string might contain characters like '.', '+* etc these need to be escaped: "HE+LO%" -> regex("HE\+LO.*").
Another example are shell globs where * and ? take the place of % and _.
One way is to write a regex to change your input syntax to a perl regex: std::string dos_wildcard_to_regex(std::string s) { static const boost::regex e("([.\\[{()\\+|^$])|(?)|(*)"); return regex_replace(s, e, "(?1\\$1)(?2.)(?3.*)", match_default | format_all); } Which relies on the "conditional search and replace" built into Boost.Regex. HTH, John.