Home > OS >  Rewrite a Regular Expression without Catastrophic Backtracking?
Rewrite a Regular Expression without Catastrophic Backtracking?

Time:02-20

I was wondering if anyone could examine this regular expression and rewrite it without catastrophic backtracking? My SonarQube seems to be upset. ;)

String NEGATIVE = "(.*?[^0-9] ?)-([0-9.] ?.*?)";

According to the Java code, it is attempting use this expression to help find and replace negative number signs with tilde characters.

// Replace any negative number signs as they will
// be confused with the subtraction operator.
expression = expression.replaceAll(NEGATIVE, "$1~$2").replaceFirst("^-", "~");

Thanks.

CodePudding user response:

In the patten (.*?[^0-9] ?)-([0-9.] ?.*?) the non greedy part .*? at the end can be omitted, as it will not match any characters.

This part [0-9.] ? will only match 1 character as the quantifier is non greedy so the quantifier can be omitted (as the following .*? does not match any character)

You can use lookarounds with just a single character in the assertions:

(?<!\d)-(?=\d)
  • (?<!\d) Negative lookbehind, assert not a digit directly to the left of the current position
  • - Match a hyphen
  • (?=\d) Positive lookahead, assert a digit directly to the right of the current positions

Regex demo

CodePudding user response:

This will match all negative signs before digits in basic expressions. Execute, and then replace the entire match with a ~.

You did not post your input set, so there might be other cases that need to be coded.

(?<=[- *\/0-9 ]|^)-(?=[\d] )

Here are some examples of replacements https://regex101.com/r/3H30tU/1https://regex101.com/r/3H30tU/1

  • Related