n 个字符或至少 z 个字符的正则表达式
我有一个输入控件(.net 正则表达式,并且必须是 1 个行),我必须确保它的长度为 15 个字符,并带有 - 。或者输入框可以有 N/A
,例如 1443-RXSW3-8738 - 这是 15 个字符长 或 N/A
到目前为止,我有这个 ^[a-zA-Z0-9]{15}$
,这只能确保它至少有 15 个字符。
是否有可能为此使用 1 行正则表达式?以下作品。我只需确保它的长度是否为 15 个字符,那么它必须有连字符。
^[a-zA-Z0-9]{15}$|(N\/A\b)
输入框必须有15个字符长并且必须有-。或者它可以有N
I have a input control (.net Regex and must be 1 liner) that I have to make sure it's either 15 characters long with - . or input box can have N/A
for example, 1443-RXSW3-8738 - this is 15 characters long
or N/A
So far I have this ^[a-zA-Z0-9]{15}$
and this only makes sure it has at least 15 character.
Is it possible to have a 1 line Regex for this? The following works. I just have to make sure if it's 15 characters long then it must have hyphen.
^[a-zA-Z0-9]{15}$|(N\/A\b)
Input box must have 15 characters long and must have -. or it can have N
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
^([a-zA-Z0-9-]{15}|N/A)$
。我们只需在字符类的末尾或开头添加连字符,以确保它被视为普通字符。尝试一下 http://regexstorm.net/tester?p=%5e%28%5ba-zA-Z0-9-%5d%7b15%7d%7cN%2fA%29%24&i=1443-RXSW3-8738 参考文献
: https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#positive-character-group--
Use
^([a-zA-Z0-9-]{15}|N/A)$
. We simply add hyphen to the character class at either the end or the start to ensure it is treated as a normal character.Try it out at http://regexstorm.net/tester?p=%5e%28%5ba-zA-Z0-9-%5d%7b15%7d%7cN%2fA%29%24&i=1443-RXSW3-8738
Reference: https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#positive-character-group--