[regex] converting a wildcard expr into a regex
Hi, 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 _. I have not found an obvious and easy way (besides doing the replacing and escaping by hand) of how to do that using Boost.Regex. Is there any support for this problem present in Boost? Thank you and best regards Christoph
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.
participants (2)
-
Christoph
-
John Maddock