如果文本只包含点而不包含其他特殊字符,如何用方括号提取文本? 例如,我想从
中提取“com.package.file” "ERR|appLogger|[Manager|Request]RequestFailed[com.package.file]uploading[com.file_upload]"
答案 0 :(得分:2)
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
}
答案 1 :(得分:0)
以下内容:
^\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));