PHP - Why am I being warned that my regular expression is too large?

前端 未结 3 1118
走了就别回头了
走了就别回头了 2021-01-02 23:36

I would like to use a regular expression to validate user input. I want to allow any combination of letters, numbers, spaces, commas, apostrophes, periods, exclamation marks

3条回答
  •  忘掉有多难
    2021-01-03 00:14

    While I agree that the regex compiler shouldn't behave that way, you really shouldn't have encountered this problem. Inside the parentheses, your regex matches exactly one character from a specific set--the definition of a character class. The correct way to write your regex is to list all the characters inside one set of square brackets and forego the parentheses:

    /^[a-z0-9 ,'.!?]{1,4000}$/i
    

    That works fine, as this demo shows. However, it was the parentheses that were causing the error (even non-capturing parens cause it), and that doesn't seem right to me, even if they were unnecessary.

提交回复
热议问题