如果文本仅包含点而没有其他特殊字符,如何提取方括号中的文本?例如我想从中提取“com.package.file”
"ERR|appLogger|[Manager|Request]RequestFailed[com.package.file]uploading[com.file_upload]"
String s = "ERR|appLogger|[Manager|Request]RequestFailed[com.package.file]uploading[com.file]";
Pattern pattern = Pattern.compile("\\[([A-Za-z0-9.]+)\\]");
Matcher m = pattern.matcher(s);
if (m.find()) {
System.out.println(m.group(1)); // com.package.file
}
类似的东西:
^\w+\|\w+\|\[\w+\|\w+\]\w+\[([\w\.]+)\]\w+\[[\w\.\_]+\]$
会让你捕捉到它。
Pattern pattern = Pattern.compile("^\\w+\\|\\w+\\|\\[\\w+\\|\\w+\\]\\w+\\[([\\w\\.]+)\\]\\w+\\[[\\w\\.\\_]+\\]$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("ERR|appLogger|[Manager|Request]RequestFailed[com.package.file]uploading[com.file_upload]");
System.out.println(matcher.group(1));