数字范围的Java正则表达式
您好,我需要一个Java正则表达式来匹配以下任一项: 1,2,3 W即:1W或2W或3W等数字范围的Java正则表达式,java,regex,numbers,range,Java,Regex,Numbers,Range,您好,我需要一个Java正则表达式来匹配以下任一项: 1,2,3 W即:1W或2W或3W等1,2,3,4,5,6,7,8,9,10,11,12 M1,2 Y 我尝试过使用以下工具,但它不能正常工作 [0-2]{1}W|[1-12]{2}M|[1-2]{1}Y 谢谢试试这个: ^([1-3]W|[1-9]M|1[0-2]M|[12]Y)$ ^([1-3]W|(1[0-2]|[1-9])M|[12]Y)$ 试试这个: ^([1-3]W|[1-9]M|1[0-2]M|[12]Y)$ ^([1-
1,2,3,4,5,6,7,8,9,10,11,12 M
1,2 Y 我尝试过使用以下工具,但它不能正常工作
[0-2]{1}W|[1-12]{2}M|[1-2]{1}Y
谢谢试试这个:
^([1-3]W|[1-9]M|1[0-2]M|[12]Y)$
^([1-3]W|(1[0-2]|[1-9])M|[12]Y)$
试试这个:
^([1-3]W|[1-9]M|1[0-2]M|[12]Y)$
^([1-3]W|(1[0-2]|[1-9])M|[12]Y)$
解释如下:
^ --> the beginning of the string
( --> group and capture to \1:
[1-3] --> any character of: '1' to '3'
W --> 'W'
| --> OR
( --> group and capture to \2:
1 --> '1'
[0-2] -->any character of: '0' to '2'
| --> OR
[1-9] --> any character of: '1' to '9'
) --> end of \2
M --> 'M'
| --> OR
[12] --> any character of: '1', '2'
Y --> 'Y'
) --> end of \1
$ --> before an optional \n, and the end of the
string
希望有帮助。我在正则表达式方面没有那么丰富的经验,但是, 试着把这三个机会分开。 例如: ([0-3]W)|((1[0-2])|([1-9])M)|([12]J) 我看不出你想在一行的开头找到这个词, 所以不需要^和$
用
[…]
测试的是字符类。它不做你认为它做的事情,它也不关心内容的大小,内容恰好是数字。