我对Regex并不擅长,并且想知道是否有人可以帮助解释如何不允许以下内容。我想阻止用户能够在字符串中输入任何表情符号以及只有标点符号。即
".." => fail
"-----" => fail
",,,," => fail
"Mc-Donald" => pass
字符串中的任何表情符号也应导致失败。
我正在考虑使用Regex为此设置自定义Laravel规则,以检查字符串是否仅包含标点符号或任何表情符号。
也许是一种更好的方法,即正则是只允许某些字符?因为我真的只想只允许字母,数字和标点符号(不仅是刺耳的刺耳),
感谢您的任何帮助!
I'm not great with RegEx and wondering if someone could help explain how I can go about not allowing the following. I want to block the user from being able to enter any emojis in a string as well as only punctuation. i.e.
".." => fail
"-----" => fail
",,,," => fail
"Mc-Donald" => pass
Any emojis in the string should also result in a fail.
I was thinking of setting up a custom Laravel rule for this using regex to check if the string contains only punctuation or any emojis.
Perhaps a better way might be for a RegEx to only allow certain characters? As I only really want to allow only letters, numbers and punctuation (just not only puncation on it's own)
Thanks for any help!
发布评论
评论(2)
查看验证规则
alpha_dash
或alpha_num
。如果您想对其进行Finetune,则可以使用以下代码自定义您自己的正则表达式:(将其添加到服务提供商),
我不会在此处解释这句话,因为它不超出范围。查找指南,您将很快学习其中的绳索。此外,您可以尝试使用用正则纠正。
Check out the validation rule
alpha_dash
https://laravel.com/docs/9.x/validation#rule-alpha-dash oralpha_num
. If you want to finetune it, you can use the following code to customize your own regex:(Add this to a service provider)
I will not explain the regex meaning here since it is out of scope. Look up a guide and you will quickly learn the ropes of that. Furthermore, you could try an online service like https://regex101.com/r/PxtN3U/1 to tinker with the regex.
有一个php函数 ctype_punct_punct 字符不是空格或字母数字字符”。
类似的事情:
更详细的验证将需要正则态度,例如,必须包含至少一个“字母顺序的字符”:
There is a php function ctype_punct that "check for any printable character which is not whitespace or an alphanumeric character".
Something like that:
More detailed validation will require regex, e.g. must contains at least one "alphabetical character":