[英]String.matches() return false unexpectedly.
I'm trying to write a program that interprets race results. 我正在尝试编写一个解释比赛结果的程序。 However in order to do this I need to use regex to test if strings match a certain pattern using the String.matches(String regex)
method. 但是,为了做到这一点,我需要使用正则表达式使用String.matches(String regex)
方法测试字符串是否与特定模式匹配。 When using this methods some strings that I expect to return true
, return false
instead. 当使用此方法时,我希望某些字符串返回true
,而是返回false
。
Using the following string as the regex pattern, some of the strings return true
and other false
when the should return true
as well: 使用以下字符串作为正则表达式模式,当the字符串也应返回true
时,其中一些字符串将返回true
而另一些则将返回false
:
regex = "[ ]*[1-9]+[ ]+[a-zA-Z]+[ ]+[a-zA-Z]+[ ]+[a-zA-Z]+[ ]?[a-zA-Z]*[ ]?[a-zA-Z]*[ ]?[a-zA-Z]*[ ]?[a-zA-Z]*[ ]?[a-zA-Z]*[ ]?[1-9]+[ ]+[1-9]+[ ]+[0-9:.]+[ ]+"
These are some of the strings that return true
with the string.matches(String regex)
method: 以下是一些使用string.matches(String regex)
方法返回true
的string.matches(String regex)
:
" 1 Ryan Kepshire Crown Point 31 4 16:31 "
" 2 Matthew Mosak Crown Point 34 4 16:44 "
" 3 Dylan Wallace Crown Point 36 4 16:58 "
" 4 Nicholas Zak Hanover Central 51 6 17:08 "
" 5 Joshua Whitaker Crown Point 37 4 17:16 "
" 8 Collin Allen Hobart 64 8 17:23 "
" 9 Zachary Hoover Crown Point 29 4 17:29 "
These are some of the strings that return false
with the string.matches(String regex)
method: 以下是一些使用string.matches(String regex)
方法返回false
的string.matches(String regex)
:
" 6 Christopher Ramos River Forest 103 12 17:18 "
" 7 Boyer Hunter Lowell 78 10 17:20 "
" 10 Miguel Tapia Merrillville 98 11 17:32 "
I need help with determining why the bottom three strings fail the regex test while the top 7 pass it. 我需要确定下三个字符串为何未通过前七个测试的正则表达式测试的帮助。
[ ]*[0-9]+[ ]+[a-zA-Z]+[ ]+[a-zA-Z]+[ ]+[a-zA-Z]+[ ]?[a-zA-Z]*[ ]?[a-zA-Z]*[ ]?[a-zA-Z]*[
^
]?[a-zA-Z]*[ ]?[a-zA-Z]*[ ]?[0-9]+[ ]+[0-9]+[ ]+[0-9:.]+[ ]+
^ ^
Your regex does not allow 0
in integers.That is why it was failing.See here. 您的正则表达式不允许整数为0
,这就是失败的原因。请参见此处。
http://regex101.com/r/uH3tP3/5 http://regex101.com/r/uH3tP3/5
I have modified it to accept 0
. 我已将其修改为接受0
。
Ex. 例如 [1-9]+
will not accept 10
. [1-9]+
不会接受10
。
" 6 Christopher Ramos River Forest 103
12 17:18 " //fails due to 103
“ 6克里斯托弗·拉莫斯河森林103
12 17:18” //由于103
而失败
" 7 Boyer Hunter Lowell 78 10
17:20 " //fails due to 10
“ 7 Boyer Hunter Lowell 78 10
17:20” //由于10
而失败
" 10
Miguel Tapia Merrillville 98 11 17:32 //fails due to 10
“ 10
Miguel Tapia Merrillville 98 11 17:32 //由于10
而失败
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.