How to convert Ruby regex to PCRE

I want to rewrite some ruby ​​code in c / C ++ code, so I want to use the regex library implemented in c. it looks like PCRE is a good choice. there are many ruby ​​regexes. how can i convert it to PCRE style automatically.

+3


source to share


2 answers


Ruby Regular Expressions are PCRE

regular expressions because PCRE supports Perl Compatible Regular Expressions and defines a specific syntax for the regular expressions that it supports.

For help on the individual functions of the PCRE regex, see the second article related to specific ruby ​​methods, see the first (as you will see, the syntax of the regexp used is the same)



EDIT: As pointed out in the comments, the regex engine used by Ruby, Onigmo

which, according to the page link, has some of the newer Perl 5.10 features. PCRE is a regular expression library and "Current PCRE implementation is roughly Perl 5.12" so there might be some inconsistencies around the edges. The syntax is much the same, but check out the Onigmo gigub page and the ruby ​​api dock to see the difference. By and large though, it's probably safe to assume that most of the regexes you translate from ruby ​​to PCRE will work without issue.

+4


source


There is one Ruby construct that can cause problems: #{code}

This can be used inside a regex and it will be replaced with the result of the code (as in double quotes).



One example: "dideldideldummdummdumm".match(/didel#{'dumm'*2}/)

+1


source







All Articles