Home > Mobile >  Writing ECMAScript Javascript Regex to Perl flavour (PCRE) / (PCRE2)
Writing ECMAScript Javascript Regex to Perl flavour (PCRE) / (PCRE2)

Time:04-07

I'm having the below Regex which is written to support the ECMAScript Javascript flavour. However, this has to be re-written in the Perl flavour (PCRE) / (PCRE2)

(?<!x)(?<!\border\D*\W*)(?<!\border\D*number\W*)(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d(?:[._ –-]*\d){4}

The above regex has Set of rules in it, Regex Demo here

I don't have an idea in Perl on how to implement it, however if i could get a help on this, it would be great!

CodePudding user response:

You may use this PCRE compliant regex equivalent of your Javascript regex:

\border(?:\s*number)?\W*\d (*SKIP)(*F)|(?<!x)(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d(?:[._ –-]*\d){4}

RegEx Demo

  • The idea of the (*SKIP)(*FAIL) trick is to consume characters that you want to avoid, and that must not be a part of the match result.
  • (*F) shorthand for (*FAIL)
  • Related