即使存在匹配项,Matcher.find()也会返回 false
我的代码:
String next_line = " I MQ (AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)"
String regex_cycle = "\\([\\s()a-zA-Z]+\\)"; #matches "(AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)"
String regex_rotorName = "^[^\\s()RNM]+"; #matches "I"
String regex_rotorDescriptor = "^[MNR][^\\s()]*"; #matches "MQ"
Matcher name_matcher = Pattern.compile(regex_rotorName).matcher(next_line);
Matcher cycle_matcher = Pattern.compile(regex_cycle).matcher(next_line);
Matcher descriptor_matcher = Pattern.compile(regex_rotorDescriptor).matcher(next_line);
System.out.println(name_matcher.find());
System.out.println(cycle_matcher.find());
System.out.println(descriptor_matcher.find());
出于某种原因,我的matcher.find()
返回 false。
如图所示,next_line
等于“I MQ (AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)。”
。
所有三个 find()
都应该根据这个字符串返回 true,对吗?
String next_line = " I MQ (AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)"
String regex_cycle = "\\([\\s()a-zA-Z]+\\)"; #matches "(AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)"
String regex_rotorName = "^[^\\s()RNM]+"; #matches "I"
String regex_rotorDescriptor = "^[MNR][^\\s()]*"; #matches "MQ"
Matcher name_matcher = Pattern.compile(regex_rotorName).matcher(next_line);
Matcher cycle_matcher = Pattern.compile(regex_cycle).matcher(next_line);
Matcher descriptor_matcher = Pattern.compile(regex_rotorDescriptor).matcher(next_line);
System.out.println(name_matcher.find());
System.out.println(cycle_matcher.find());
System.out.println(descriptor_matcher.find());
For some reason, my matcher.find()
's are returning false.
As seen in the image, next_line
equals " I MQ (AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)."
.
All three find()
s should return true based on this string right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎在 System.out 语句中的
if
之前调用find()
,以便它们报告第二个匹配项,而不是第一个匹配项。分配给局部变量并使用 System.out.println 和 if 中的值
You appear to be calling
find()
in System.out statements immediately before theif
so that they are reporting the second matches, not first match.Assign to local variable and use the values in
System.out.println
andif