我在Java正则表达式中找到了一段代码,这让我感到困惑:
Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" );要编译的字符串是:
String string1 = "Jane's Birthday is 05-12-75\n" + "Dave's Birthday is 11-04-68\n" + "John's Birthday is 04-28-73\n" + "Joe's Birthday is 12-17-77";这意味着什么?
[0-35-9]为什么有4“d”而不是3?我想生日的时候只有三个号码。
发布于 2013-11-08 08:17:02
\\d的形式只是匹配一个数字,而不是一个数字。
因此,使用\\d\\d模式将匹配两个连续数字。
使用\\d\\d-\\d\\d将匹配两个连续数字,一个-字面上是两个连续数字。
让我们看看你的对手和原因。
Joe's Birthday is 12-17-77
^ match a digit 0 to 9
^ match any character of '0' to '3', '5' to '9'
^ match a '-' literally
^ match a digit 0 to 9
^ match a digit 0 to 9
^ match a '-' literally
^ match a digit 0 to 9
^ match a digit 0 to 9[0-35-9]部件将0的任何字符与3匹配,5与9匹配。
你的常规表达方式解释道:
J 'J'
.* any character except \n (0 or more times)
\d match a digit 0 to 9
[0-35-9] any character of: '0' to '3', '5' to '9'
- match a '-' literally
\d match a digit 0 to 9
\d match a digit 0 to 9
- match a '-' literally
\d match a digit 0 to 9
\d match a digit 0 to 9发布于 2013-11-08 04:16:31
\\d不匹配一个数字,它匹配一个数字。区别是\\d\\d将匹配两个连续数字。
[0-35-9]将匹配范围0-3中的数字或范围5-9中的数字。
实际的结果是这个月的生日是10,11,12,01,02,03,05,06,07,08或09。日期和年份并不重要,只要它们是两位数。这是一种长篇大论的说法:“给我找找任何不在四月(04)的生日”。
发布于 2013-11-08 04:18:49
https://stackoverflow.com/questions/19851462
复制相似问题