我的代码:
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,对吗?
您似乎在if
之前的System.out语句中调用find()
,以便它们报告第二个匹配,而不是第一个匹配。
分配给局部变量并使用System.out.println
和if
中的值