[英]RegEx pattern to detect two consecutive numbers followed by at the most 9 characters?
How can I create a regex that detects any string starting with two consecutive numbers followed by at the most nine consecutive characters with a hyphen symbol in java regex? 如何在Java正则表达式中创建一个正则表达式来检测任何字符串,该字符串以两个连续的数字开头,然后是最多9个带有连字符的连续字符? For instance:
例如:
nnccccccccc-nnccccccccc
or 要么
nncccccc-nnccccccccc
or 要么
nnccccccc-nncccccccc
Where n
represents a number from 0 to 1 and c
a letter character. 其中
n
表示从0到1的数字, c
表示字母字符。
So far I tried this: https://regex101.com/r/a1eJvY/2 . 到目前为止,我已经尝试过: https : //regex101.com/r/a1eJvY/2 。
You can use ^(\\d{2}[a-zA-Z]{0,9})-(\\d{2}[a-zA-Z]{0,9})$
example: https://regex101.com/r/A2wiHH/2 . 您可以使用
^(\\d{2}[a-zA-Z]{0,9})-(\\d{2}[a-zA-Z]{0,9})$
示例: https:// regex101.com/r/A2wiHH/2 。
This will match the string as described below: 这将匹配字符串,如下所述:
2
decimals 2
小数 0-9
characters 0-9
字符 -
, -
, 2
decimals again, 2
小数, 0-9
characters again 0-9
字符 You may use this regex for your matches: 您可以使用此正则表达式进行匹配:
^\d{1,2}[a-zA-Z]{1,9}-\d{1,2}[a-zA-Z]{1,9}$
If you are using .matches()
method then ^
and $
are not needed. 如果使用
.matches()
方法,则不需要^
和$
。
\\d{1,2}
: Match 1 or 2 digits \\d{1,2}
:匹配1或2位数字 [a-zA-Z]{1,9}
: Match 1 to 9 English letters [a-zA-Z]{1,9}
:匹配1到9个英文字母 -
: Match literal hyphen -
:匹配文字连字符 I suppose that "nn" and no one letter char is accepted, so, for a single sequence: 我想“ nn”且不接受任何一个字符char,因此对于单个序列:
[0,1]{2}\D{0,9}
Explaination: 说明:
[0,1]{2}
--> Accept only 0 and 1 as number exactly two times; [0,1]{2}
->仅接受0和1作为数字正好两次;
\\D{0,9}
--> Accept 0 to 9 generic number. \\D{0,9}
->接受0到9个通用数字。
Edit: you said 编辑:你说
Where n represents a number from 0 to 1
其中n表示从0到1的数字
but if 22may
is accepted, you want number from 0 to 9, so you have to use \\d
但是如果接受
22may
,则您希望数字从0到9,因此必须使用\\d
\d{2}\D{0,9}
Try this [0-1] {2}+ [az] {9}. 试试这个[0-1] {2} + [az] {9}。 +-
+-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.