简体   繁体   中英

regular expression to limit 300 words

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?

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 .

您可以使用String.split并检查返回的数组的大小。

如果第300th单词是最后一个单词并且前面没有space ,那么你的正则表达式将失败。你应该使用

^ *(?:\S+(?: +|$)){0,300} *$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM