3

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

/ing$|ed$|en$/

...

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Yadollah
  • 77
  • 8

2 Answers2

3

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")

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
  • What about this perl expression: '/ O$/' What is the meaning of $ in end of perl expression? Another question is: When I write for example expression "^B_" in boost, what will mean? – Yadollah Apr 30 '10 at 17:09
2
/^[\.:\,()\'\`-]/

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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    The escape isn't need inside character classes (`[...]`) in Perl, either. – mob Mar 29 '10 at 14:48
  • Right. They were unnecessary to begin with. In some cases, unnecessary backslashes can even become syntax errors (`\<` for example). – Tim Pietzcker Mar 29 '10 at 14:55