Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Ruby Regular Expression into PCRE

Tags:

regex

ruby

pcre

I want to rewrite some ruby code into c/c++ code, so I want to use a regular expression library implemented in c. it seems that PCRE is a good choice. there are many ruby style regular expressions. how can i convert it into PCRE style automatically.

like image 940
WhatisThat Avatar asked Aug 05 '14 08:08

WhatisThat


1 Answers

Ruby regular expressions are PCRE regular expressions as PCRE stands for Perl Compatible Regular Expressions and defines a particular syntax for the regular expressions it supports.

For help on individual PCRE regex features see the second article I linked to and for ruby specific methods see the first (as you will see, the syntax of the actual regex's used are the same)

EDIT: as pointed out in the comments the regex engine used by ruby is Onigmo which according to the linked to page has some of the new features of Perl 5.10. PCRE is a regex library and "The current implementation of PCRE corresponds approximately with Perl 5.12" so there may be some discrepancies around the edges. The syntax is largely the same though, look through the Onigmo github page and ruby api dock to see the difference. By and large though it is probably safe to assume that most regexes that you translate from ruby to PCRE will work without problems.

like image 143
Mike H-R Avatar answered Sep 20 '22 17:09

Mike H-R