简体   繁体   English

用于允许字母数字、特殊字符且不以 @ 或 _ 或结尾的正则表达式

[英]regex for allowing alphanumeric, special characters and not ending with @ or _ or

I am new to regex , I created below regex which allows alpha numeric and 3 special characters @._ but string should not end with @ or .我是 regex 的新手,我在 regex 下方创建了它,它允许使用字母数字和 3 个特殊字符 @._ 但字符串不应以 @ 或 . or *要么 *

^[a-zA-Z0-9._@]*[^_][^.][^@]$

it validates abc@ but fails for abc.它验证 abc@ 但对 abc 失败。

Your pattern allows at least 3 characters, where the last 3 are negated character classes matching any char other than the listed.您的模式允许至少 3 个字符,其中最后 3 个是与除所列字符以外的任何字符匹配的否定字符类。

The pattern ^[a-zA-Z0-9._@]*[^_][^.][^@]$ will match 3 newlines , and adding all the chars to a single character class ^[a-zA-Z0-9._@]*[^@._]$ will also match a single newline only.模式^[a-zA-Z0-9._@]*[^_][^.][^@]$匹配 3 个换行符,并将所有字符添加到单个字符类^[a-zA-Z0-9._@]*[^@._]$也将仅匹配单个换行符


If you want to allow all 3 "special" characters and match at least 3 characters in total you can repeat the character class 2 or more times using {2,} and match a single char at the end without the special characters.如果您想允许所有 3 个“特殊”字符并且总共匹配至少 3 个字符,您可以使用{2,}重复字符类 2 次或更多次{2,}并在末尾匹配一个没有特殊字符的字符。

^[a-zA-Z0-9._@]{2,}[a-zA-Z0-9]$

Regex demo正则表达式演示

Matching as least a single char (and not end with . _ @ )匹配至少一个字符(并且不以. _ @结尾)

^[a-zA-Z0-9._@]*[a-zA-Z0-9]$

Regex demo正则表达式演示

Leading ^ is the start of the paragraph前导 ^ 是段落的开头
Trailing $ is the end of the paragraph尾随 $ 是段落的结尾
. . is the everything {2,} means the more than 2 letters是一切 {2,} 意味着超过 2 个字母
[^@_] means one letter Not @ or _ [^@_] 表示一个字母不是@或_

^.{2,}[^@_]$

click here the answer点击这里的答案

if you include all the characters in the one character set, that'll work.如果您将所有字符都包含在一个字符集中,那将起作用。

^[a-zA-Z0-9._@]*[^@._]$

在此处输入图片说明

Screenshot shows how different text examples would work (try it out on http://regexr.com )屏幕截图显示了不同的文本示例如何工作(在http://regexr.com上试用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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