您需要使用组匹配来解析所需的数据
String result = null;
try {
Pattern regex = Pattern.compile("\\s*\\w+\\s*\\w+\\s*([\\w| ]+)");
Matcher regexMatcher = regex.matcher(" AAA BBB DDDD DDDD DDDDD");
if (regexMatcher.find()) {
result = regexMatcher.group(1); // result = "DDDD DDDD DDDDD"
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
正则表达式解释
"\\s" + // Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"\\w" + // Match a single character that is a “word character” (letters, digits, and underscores)
"+" + // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"\\s" + // Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"\\w" + // Match a single character that is a “word character” (letters, digits, and underscores)
"+" + // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"\\s" + // Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"(" + // Match the regular expression below and capture its match into backreference number 1
"[\\w| ]" + // Match a single character present in the list below
// A word character (letters, digits, and underscores)
// One of the characters “| ”
"+" + // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")"