简体   繁体   中英

Matcher.find() returns false even though a match exists

My code :

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?

You appear to be calling find() in System.out statements immediately before the if so that they are reporting the second matches, not first match.

Assign to local variable and use the values in System.out.println and if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM