Regular expression is very slow on failure

I have a regex that should check if a string consists of strings separated by spaces. The regex works well (well, it allows blank space at the end ... but that is not a problem), but takes too long when the validation fails.

The regular expression is as follows:

/^(([\w\-]+)( )?){0,}$/

When trying to check with string

"'this-is_SAMPLE-scope-123,this-is_SAMPLE-scope-456'"

It takes 2 seconds.

The tests were carried out in ruby ​​1.9.2-rc1 and 1.8.7. But this is probably a common problem.

Any idea?

+3
source share
1 answer

. :

(.+)*

+ * .

, , - :

^[\w\-]+( [\w\-]+)*$

( rubular.com):

hello world
99 bottles of beer on the wall
this_works_too

:

not like this, not like this
hey what the &#@!
too many    spaces

- / .


{0,} *. , , , .. (?:pattern).

+8

Source: https://habr.com/ru/post/1542267/


All Articles