将代码与固定国家代码和可变通配符使用的正则匹配
我需要实施一个涵盖几个要求的正则表达式。这些是以下内容:
- 应完成对最大8个字符的长度限制(使用或 没有通配符)。无论如何,代码永远不会超过8个字符。
- 当给出通配符时,也允许8位数字。没有 通配符恰好需要8位数字。
- 允许的字符为: 0-9a-Za-Z *(所有数字,所有字符,asterix as asterix as wildcard)
- 纯通配符,否则
- 前两个数字必须包含2个字符国家代码 (alpha-numeric),然后仅数字或通配符。
- 在国家代码通配符之后,可以在任何地方使用(最终,最终,Mutliple Asterix/oft Checters offere也允许),
我到目前为止尝试了很多事情,并考虑了由于Asterix和Max的lookahead/lookbehind的考虑。长度。 我的最新状态涵盖了最多的要求:
^([A-Za-z]{2}[0-9*]{0,6}|\*)$
但在此示例中,如果不到8个字符 - >那是错误的。
非常感谢您提前提供的任何帮助:)
I need to implement a regex which cover several requirements. These are the following:
- A length restriction to max 8 chars should be done (with or
without wildcard). In any case the code is never longer than 8 chars. - When wildcard is given also lower then 8 digits is allowed. Without
wildcard exactly 8 digits are needed. - allowed characters are: 0-9A-Za-z* (all digits, all chars, asterix as wildcard)
- pure wildcard must be possible
- else the first two digits must contain a 2 chars country code
(alpha-numeric) and then only number or wildcards are allowed. - after country code wildcard can be used at any place (in the middle, at the end, mutliple asterix/wildcards after each other also allowed)
I tried many things so far and thought about Lookahead/Lookbehind because of the asterix and the max. length.
My latest state which covers the most of the requirements is the following:
^([A-Za-z]{2}[0-9*]{0,6}|\*)$
check the live demo with right/wrong combo
But in this example a code without asterix/wildcard is possible with less than 8 chars -> that's wrong.
Thanks a lot for any help in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
regex demo 。
详细信息:
^
- 字符串的开始(?!。*\*\*$)
- 否两个** >在字符串的末尾允许
(?!。{9})
- 该字符串必须包含小于9个字符,除了line break break chars(?:[a-za-z] { 2}(?:\ d*(?:\*\ d*)+| \ d {6})| \*)
- 两个替代方案之一:[a-za-z] {2}(?:\ d*(?\*\ d*)+| \ d {6})
- 两个字母,然后是六个字母数字或零或更多数字,然后使用一个或多个星号的序列,零或更多数字|
- 或\*
- 星号$
-细绳。You can use
See the regex demo.
Details:
^
- start of string(?!.*\*\*$)
- no two**
at the end of string allowed(?!.{9})
- the string must contain less than 9 chars other than line break chars(?:[A-Za-z]{2}(?:\d*(?:\*\d*)+|\d{6})|\*)
- one of the two alternatives:[A-Za-z]{2}(?:\d*(?:\*\d*)+|\d{6})
- two letters and then either six digits or zero or more digits followed with one or more sequences of an asterisk and zero or more digits|
- or\*
- an asterisk$
- end of string.