Home > Blockchain >  Improving the performance of an PRCE Regex Pattern
Improving the performance of an PRCE Regex Pattern

Time:04-08

I have the below regex here which is written to support the PRCE/PRCE2 format. However, this throws the following error “Evaluation takes too long. Please check your regular expression.” Is there any way we can improve the performance of this regex by simplifying it?

Also, the regex throws "catastrophic backtracking" error as well.

(\border\D*\W*)\d (*SKIP)(*F)|(\border\D*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 here

The above regex has set of rules in it. Please find the requirements of the regex.

  1. First 5 numbers should only be masked in a 9 digit number.
  2. Should not mask any numbers if the 'x' or 'X' precedes the 9 digit number.
  3. If the "order" or "order number" string precedes the 9 digit numbers, then it should not be matched.
  4. You can find the list of use cases for the same along with the rules in this link. Usecases with requirements

Regex101 is giving me the exact output as expected but it has a performance issue. Need to simplify it.

CodePudding user response:

You may try this refactored regex:

\b(?>x|order(?>[\W_]*number)?[\W_]*)\d (*SKIP)(*F)|(?=(?>[._ –-]*\d){9})(?>(?>9|6{3}|0{3}|(?>\d\D*){3}00|(?>\d\D*){5}0{4})(*SKIP)(*F)|\d(?>\D*\d){4})

RegEx Demo

Compared to your existing demo link it is taking almost half number of steps in the demo link.

  • Related