1

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.

3
  • 3
    regex seems overkill, have you thought about using testString.split("=") on each line of this file and checking that length ==2? That will return an array and the second item will be the right side of the = Commented Dec 2, 2010 at 5:01
  • That's the crude way to do it. I'm leaving it as my last resort. Commented Dec 2, 2010 at 5:10
  • 1
    There's too much backtracking allowed in your regex so you're probably getting stack overflows if it doesn't match quickly. Try to avoid nesting *s, or see if you can make them possessive - you want to help it fail faster. Commented Dec 2, 2010 at 5:17

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

your pattern does not give me the RHS of "=" in the string "something.operer= asda adad" as it has space in between words. My second * was to take care of these instances. And I will have only one match per string hence I could use "$"
Ah, well if spaces are allowed on the right side, simply move the \\s into the character class instead of adding another *. You only need repeating groups if the pattern is more strict than that. Updated my answer too.
I already did that, was about to update here. Thanks anyway for the help.
That equals does not need to be escaped. Also, this pattern only works for ASCII.
@tchrist The equals doesn't need to be escaped? What kind of feedback is that? Who cares? It has a functional equals sign immediately preceding it, so I escaped it A) to be safe, B) to be less confusing. So since I'm obviously not the expert here, maybe you can tell me what the negatives are?
|
0

Try this:

=\\s*(.*)$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.