Reputation: 1775
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
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 :
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