2

如果文本仅包含点而没有其他特殊字符,如何提取方括号中的文本?例如我想从中提取“com.package.file”

 "ERR|appLogger|[Manager|Request]RequestFailed[com.package.file]uploading[com.file_upload]"
4

2 回答 2

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
}
于 2012-08-22T12:31:59.833 回答
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));
于 2012-08-22T12:31:22.713 回答