kaychaks
kaychaks

Reputation: 1775

Java Regex Engine Crashing

Regex Pattern - ([^=](\\s*[\\w-.]*)*$)

Test String - paginationInput.entriesPerPage=5

Java Regex Engine Crashing / Taking Ages (> 2mins) finding a match. This is not the case for the following test inputs:

paginationInput=5

paginationInput.entries=5

My requirement is to get hold of the String on the right-hand side of = and replace it with something. The above pattern is doing it fine except for the input mentioned above.

I want to understand why the error and how can I optimize the Regex for my requirement so as to avoid other peculiar cases.

Upvotes: 1

Views: 234

Answers (2)

Nicole
Nicole

Reputation: 33197

You can use a look behind to make sure your string starts at the character after the =:

(?<=\\=)([\\s\\w\\-.]*)$

As for why it is crashing, it's the second * around the group. I'm not sure why you need that, since that sounds like you are asking for :

  • A single character, anything but equals
  • Then 0 or more repeats of the following group:
    • Any amount of white space
    • Then any amount of word characters, dash, or dot
  • End of string

Anyway, take out that *, and it doesn't spin forever anymore, but I'd still go for the more specific regex using the look behind.

Also, I don't know how you are using this, but why did you have the $ in there? Then you can only match the last one in the string (if you have more than one). It seems like you'd be better off with a look-ahead to the new line or the end: (?=\\n|$)

[Edit]: Update per comment below.

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

Try this:

=\\s*(.*)$

Upvotes: 0

Related Questions