我正在处理正则表达式的问题。
我有一个 maxLength 10 的输入。
到目前为止,我实现了第一个给定值可以是数字,例如 12345,但随后它等待破折号,在它之后您可以写一个字母或再次写一个数字 maxLength=10 例如: 12345-a121 是允许的,它适用于当前的
但我希望在 5 位数字之后可以允许字母或破折号,因为目前使用此正则表达式,只允许在后面加上破折号5 位数字。
例如 12345a 或 12345- 是允许的。
这是我正在使用的实际正则表达式。
Valid/Matches: 12345a235, 123a, 12345-aa1, 12345a, 12345-a.
Not Valid/Does not matches: -11, 111111, aaaa,
(?=^[^W_]{1,5}-[^W_]{1,8}$)^.{1,10}$|^[^W_]{1,5}$
我正在 regex101.com 上进行调试,但我没有找到允许的方法。
例如12345a
这是检查是否匹配的条件。
if (!this.value.toString().match('^\d{1,5}(?!\d+)[-\p{L}\d]+$') && this.value.toString()) {
return ValidationInfo.errorCode("You need to do something");
谢谢你的帮助
I am dealing with an issue with Regex.
I have a input which has maxLength 10.
I achieved till now to have the first given value can be digits for example 12345 but then it waits for a dash and after it you can write a letter or again a number maxLength=10 for example: 12345-a121 is allowed and it works with the currrent
But I want to be possible after the 5 digits to be allowed letters or dash because for the moment with this regex it is allowed only dash after 5 digits.
For example 12345a or 12345- to be allowed.
This is the actual regex what I am using.
Valid/Matches: 12345a235, 123a, 12345-aa1, 12345a, 12345-a.
Not Valid/Does not matches: -11, 111111, aaaa,
(?=^[^W_]{1,5}-[^W_]{1,8}$)^.{1,10}$|^[^W_]{1,5}$
I am debugging on the regex101.com but I am not finding a way for that to allow.
12345a for example
This is the condition to check if it matches or not.
if (!this.value.toString().match('^\d{1,5}(?!\d+)[-\p{L}\d]+
Thank you for the help
) && this.value.toString()) {
return ValidationInfo.errorCode("You need to do something");
Thank you for the help
发布评论
评论(1)
编辑,因为第一种方法的模式可以简化,并且也缺少结束序列长度的限制。
仅与
Letter unicode 属性转义
/^\d{1,5}[-\p{L}][ -\p{L}\d]{0,9}$/u
使用
Letter进行匹配和捕获 unicode 属性逃脱
<代码>/^(?\p{N}{1,5})(?<杂项>[-\p{L}][-\p{L}\p{N }]{0,9})$/u
示例代码...
第一种方法
简单明了...
/^\d{1,5}(?!\d+)[-\p{L}\d]+$/u
带有命名捕获组...
/^(?\p{N}{1,5}(?!\p{N}+))(?<杂项>[-\p{ L}\p{N}]+)$/u
对于两者变体很明显以...开头,
^\d{1,5}
这也很清楚,一个人想要以任何一个字符序列结束短跑和/或词。由于必须排除
_
,因此不能仅使用\w
转义字母和数字,因为\w
覆盖/包含_
也是如此。但是可以使用 unicode 属性转义,因此...[-\p{L}\d]+$
[-\p{L}\p{N}]+)$
组合正则表达式如 ...
/^\d{1,5}[-\p{L}\d]+$/u
... 几乎涵盖了要求,但失败了111111
哪个原因得到匹配,即使它不应该符合到要求。负向预测 ...
(?!\d+)< /code> 分别
(?!\p{N}+)
...紧随起始数字序列确实会阻止任何其他(终止)纯数字序列,因此123456
未匹配不再了。示例代码...
Edit since the patterns of the first approach can be simplified and also were missing the limitations of the ending sequence's length.
for matching only with
Letter
unicode property escapes/^\d{1,5}[-\p{L}][-\p{L}\d]{0,9}$/u
matching and capturing with
Letter
unicode property escapes/^(?<digitsOnly>\p{N}{1,5})(?<miscellaneous>[-\p{L}][-\p{L}\p{N}]{0,9})$/u
Example code ...
1st approach
straightforward and plain ...
/^\d{1,5}(?!\d+)[-\p{L}\d]+$/u
with named capture groups ...
/^(?<digitsOnly>\p{N}{1,5}(?!\p{N}+))(?<miscellaneous>[-\p{L}\p{N}]+)$/u
For both variants it is obvious to start with ...
^\d{1,5}
^\p{N}{1,5}
It's also clear, one wants to end with a character sequence of any of dash and/or word. Due to having to exclude
_
one can not just use the\w
escape for letters and digits since\w
covers/includes_
as well. But one could use unicode property escapes, thus ...[-\p{L}\d]+$
[-\p{L}\p{N}]+)$
A combined regex like ...
/^\d{1,5}[-\p{L}\d]+$/u
... almost covers the requirements but fails for111111
which of cause gets matched even though it shouldn't according to the requirements.A negative lookahead ...
(?!\d+)
respectively(?!\p{N}+)
... which follows the starting digit sequence does prevent any other (terminating) digit-only sequence, thus123456
does not get matched anymore.Example code ...