1

I need a regex which success 0-300 words and fails 301 or more words.

I tried:

^\s*(\S+\s+){0,300}\S*$

I also checked

^\W*(?:\w+\b\W*){0,300}$

Both are working fine but in Java I get a java.lang.StackOverflowError. I know using a larger "XSS" I get around this issue but I wanted to ask if there is a way to optimize the regex?

Braj
  • 46,415
  • 5
  • 60
  • 76
user1635689
  • 69
  • 2
  • 10
  • 5
    *"Both are working fine but in Java I get a "java.lang.StackOverflowError""* Um, how can they be "working fine" **and** be throwing an exception? – T.J. Crowder Mar 11 '15 at 08:14
  • Sorry you're right... Both are doing the job fine if I test the in an online regex tool such as https://regex101.com/ but in Java they are not working – user1635689 Mar 11 '15 at 08:34
  • Then you'll need to show your code using it, as that's likely to be the problem. – T.J. Crowder Mar 11 '15 at 08:52

3 Answers3

2

I believe the problem is because the Java implementation of Pattern uses up a stack for each repetition of a group because of backtracking. The solution might be to either change your approach as others have answered or to make all quantifiers possessive :

^\s*(\S+\s+){0,300}+\S*$

or

^\W*(?:\w+\b\W*){0,300}+$

For more info, see here or here.

Community
  • 1
  • 1
buftlica
  • 245
  • 1
  • 8
1

You can use String.split and check the size of the returned array.

CompuChip
  • 9,143
  • 4
  • 24
  • 48
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Your regex will fail if the 300th word is the last word and there is notspace ahead of it.You should use

^ *(?:\S+(?: +|$)){0,300} *$
vks
  • 67,027
  • 10
  • 91
  • 124