我试图弄清楚什么正则表达式将提取这些命名组,每个组都在单独的行上。尝试了不同的组合,但被卡住了。例如,我无法提取 localityDefault 但得到:
有数据假
模式匹配7() 空
public static void main(String[] args) throws Exception {
String patternOfData = ".*?orgUnit1Default=(?<orgUnit1Default>\\S+)\\s+" +
"orgUnit2Default=(?<orgUnit2Default>\\S+)\\s+" +
"organizationDefault=(?<organizationDefault>\\S+)\\s+" +
"localityDefault=(?<localityDefault>\\S+)\\s+?" +
"stateDefault=(?<stateDefault>\\S+)\\s+?" +
"countryDefault=(?<countryDefault>\\S+)\\s+";
String receivedDataString = "orgUnit1Default=window washers \n"
+ "orgUnit2Default=All Cleaners \n"
+ "organizationDefault=Cleaning Dept \n"
+ "localityDefault=Small Town Here\n"
+ "stateDefault=Washington\n"
+ "countryDefault=US";
String toExtractName = "organizationDefault";
System.out.println("patternMatch7() "
+ patternMatch7(patternOfData, receivedDataString,
toExtractName));
}
static String patternMatch7(String patternOfData,
String receivedDataString, String toExtractName) throws IOException {
Pattern dataExtractionPattern;
dataExtractionPattern = Pattern.compile(patternOfData, Pattern.DOTALL);
Matcher matcher = dataExtractionPattern.matcher(receivedDataString);
boolean hasData = false;
String dataValue = null;
if (matcher.find()) {
hasData = true;
dataValue = matcher.group(toExtractName);
}
System.out.println("hasData " + hasData);
return dataValue;
}