正则表达式:按递增顺序的 5 位数字 [英] Regex: 5 digits in increasing order
本文介绍了正则表达式:按递增顺序的 5 位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个按升序排列的 5 位正则表达式,例如 12345
、24579
、34680
等.
I need a regex for 5 digits in increasing order, like 12345
, 24579
, 34680
, and so on.
0
在 9
之后.
推荐答案
你可以试试 (如上 rubular.com)
^(?=\d{5}$)1?2?3?4?5?6?7?8?9?0?$
说明
^
和$
分别是字符串锚点的开头和结尾\d{5}
是完全重复{5}
次 的数字字符类(?=...)
是正向预测?
在每个数字上使每个可选^
and$
are the beginning and end of string anchors respectively\d{5}
is the digit character class\d
repeated exactly{5}
times(?=...)
is a positive lookahead?
on each digit makes each optional- 首先我们使用lookahead来断言锚定在字符串的开头,我们可以看到
\d{5}
直到字符串的结尾 - 既然我们知道我们有 5 个数字,我们只需按照我们想要的顺序匹配数字,但让每个数字都是可选的
- 断言确保我们有正确的位数
- 锚、字符类、有限重复、Lookarounds 和 可选
- Anchors, Character Classes, Finite Repetition, Lookarounds, and Optional
假设我们需要匹配包含以下内容的字符串:
Let's say that we need to match strings that consists of:
- 介于 1-3 个元音之间
[aeiou]
- 并且元音必须按顺序出现
然后模式是(如在 rubular.com 上看到的):
^(?=[aeiou]{1,3}$)a?e?i?o?u?$
同样,它的工作方式是:
Again, the way it works is that:
- 锚定在字符串的开头,我们首先断言
(?=[aeiou]{1,3}$)
- 字符串中正确的字母和正确的长度
如果每个数字都可以重复,例如
11223
是匹配项,则:If each digit can repeat, e.g.
11223
is a match, then:- 而不是每个数字上的
?
(零或一), - 我们使用
*
(零次或多次重复)
- instead of
?
(zero-or-one) on each digit, - we use
*
(zero-or-more repetition)
也就是说,模式是(如在 rubular.com 上看到的):
That is, the pattern is (as seen on rubular.com):
^(?=\d{5}$)1*2*3*4*5*6*7*8*9*0*$
这篇关于正则表达式:按递增顺序的 5 位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
\d
Explanation
查看全文