Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a Perl regex to work with Boost::Regex?

What is the Boost::Regex equivalent of this Perl regex for words that end with ing or ed or en?

/ing$|ed$|en$/

...

like image 206
Yadollah Avatar asked Dec 20 '25 08:12

Yadollah


2 Answers

The most important difference is that regexps in C++ are strings so all regexp specific backslash sequences (such as \w and \d should be double quoted ("\\w" and "\\d")

like image 190
Leon Timmermans Avatar answered Dec 22 '25 22:12

Leon Timmermans


/^[\.:\,()\'\`-]/

should become

"^[.:,()'`-]"

The special Perl regex delimiter / doesn't exist in C++, so regexes are just a string. In those strings, you need to take care to escape backslashes correctly (\\ for every \ in your original regex). In your example, though, all those backslashes were unnecessary, so I dropped them completely.

There are other caveats; some Perl features (like variable-length lookbehind) don't exist in the Boost library, as far as I know. So it might not be possible to simply translate any regex. Your examples should be fine, though. Although some of them are weird. .*[0-9].* will match any string that contains a number somewhere, not all numbers.

like image 44
Tim Pietzcker Avatar answered Dec 22 '25 22:12

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!