Products
GG网络技术分享 2025-03-18 16:15 0
I\'m trying to apply a regex constraint to a Symfony form input. The requirement for the input is that the start of the string and all commas must be followed by zero or more whitespace, then a #
or @
symbol, except when it\'s the empty string.
As far as I can tell, there is no way to tell the constraint to use preg_match_all
instead of just preg_match
, but it does have the ability to negate the match. So, I need a regular expression that preg_match
will NOT MATCH for the given scenario: any string containing the start of the string or a comma, followed by zero or more whitespace, followed by any character that is not a # or @ and is not the end of the string
, but will match for everything else. Here are a few examples:
preg_match(..., \'\'); // No matchpreg_match(..., \'#yolo\'); // No match
preg_match(..., \'#yolo, #swag\'); // No match
preg_match(..., \'#yolo,@swag\'); // No match
preg_match(..., \'#yolo, @swag,\'); // No match
preg_match(..., \'yolo\'); // Match
preg_match(..., \'swag,#yolo\'); // Match
preg_match(..., \'@swag, yolo\'); // Match
I would\'ve thought for sure that /(^|,)\\s*[^@#]/
would work, but it\'s failing in every case with 1 or more spaces and it appears to be because of the asterisk. If I get rid of the asterisk, preg_match(\'/(^|,)\\s[^@#]/\', \'#yolo, @swag\')
does not match (as desired) when there\'s exactly once space, but as as soon as I reintroduce the asterisk it breaks for any quantity of spaces > 0.
My theory is that the regex engine is interpreting the second space as a character that is not in the character set [@#]
, but that\'s just a theory and I don\'t know what to do about it. I know that I could create a custom constraint to use preg_match_all
instead to get around this, but I\'d like to avoid that if possible.
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我正在尝试将正则表达式约束应用于Symfony表单输入。 对输入的要求是字符串的开头和所有逗号必须后跟零或更多的空格,然后是#</ code>或
@ </ code>符号,除非它是空的 字符串。</ p>
据我所知,没有办法告诉约束使用 preg_match_all </ code>而不仅仅是
preg_match </ code>, 但它确实有能力否定比赛。 所以,我需要一个正则表达式
preg_match </ code>将不匹配给定的场景:
包含字符串开头或逗号的任何字符串,后跟零个或多个空格,后跟任何字符 这不是#或@并且不是字符串</ code>的结尾,但会匹配其他所有内容。 以下是一些示例:</ p>
preg_match(...,\'\'); //没有匹配preg_match(...,\'#yolo\'); //没有匹配
preg_match(...,\'#yolo,#swag\'); //没有比赛
preg_match(...,\'#yolo,@ swag\'); //没有匹配
preg_match(...,\'#yolo,@ swag,\'); //没有匹配
preg_match(...,\'yolo\'); //匹配
npreg_match(...,\'swag,#yolo\'); //匹配
preg_match(...,\'@ swag,yolo\'); //匹配
</ code> </ pre>
我肯定会想到 /(^ |,)\\ s * [^ @#] / </ code> 会工作,但它在每种情况下失败,有一个或多个空格,它似乎是因为星号。 如果我摆脱了星号, preg_match(\'/(^ |,)\\ s [^ @#] /\',\'#yolo,@ swag\')</ code>与(当需要时)不匹配 那里只有一次空间,但是当我重新引入星号时,它会打破任何数量的空间&gt; 0。</ p>
我的理论是正则表达式引擎将第二个空格解释为不在字符集 [@#] </ code>中的字符,但这只是 一个理论,我不知道该怎么做。 我知道我可以使用 preg_match_all </ code>创建一个自定义约束来解决这个问题,但是如果可能的话我想避免这种情况。</ p>
</ div>
网友观点:
You may use
\'~(?:^|,)\\s*+[^#@]~\'
Here, the +
symbol defines a *+
possessive quantifier matching 0 or more occurrences of whitespace chars, and disallowing the regex engine to backtrack into \\s*
pattern if [^@#]
cannot match the subsequent char.
See the regex demo.
Details
(?:^|,)
- either start of string or ,
\\s*+
- zero or more whitespace chars, possessively matched (i.e. if the next char is not matched with [^#@]
pattern, the whole pattern match will fail)[^@#]
- a negated character class matching any char but @
and #
.
PHP正则表达式核心技术完全详解 第4节 php正则查找匹配处理函数使用技巧与学习心得
作者:极客小俊 一个专注于web技术的80后
我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人!
知乎@极客小俊,官方首发原创文章
Bilibili: 极客小俊GeekerJun
在我们说php系统自带的正则处理函数之前,我们先要回忆一下在PHP中正则表达式的组成元素有哪些? 如下:
- 定界符号
- 原子
- 元字符、量词
- 模式修正符
例如:一个匹配URL的正则表达式如下
Demand feedback