我有这样的字符串:
10:11:22 [UTP][ROX][ID:32424][APP STR]
我想把它们各自分开。我怎样才能使用正则表达式?
我想分开" 10:11:22
"," UTP
"," ROX
", " ID:32424
"," APP STR
"作为字符串。
答案 0 :(得分:1)
我能想到的最简单的解决方案,其缺点是会创建一个空白的最终条目,是这样的:
"10:11:22 [UTP][ROX][ID:32424][APP STR]".split("[\[\]]+")
这将返回一个数组:
["10:11:22",
"UTP",
"ROX",
"ID:32424",
"APP STR",
""]
答案 1 :(得分:1)
这将是您的匹配模式:/\[([^\]]+)/g
工作演示@ regex101.com
使用Java演示:
public class Main {
private static final String REGEX = "\\[([^\\]]+)";
private static final String INPUT = "10:11:22 [UTP][ROX][ID:32424][APP STR]";
private static Pattern pattern;
private static Matcher matcher;
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
while (matcher.find()) {
System.out.println(matcher.toString());
}
}
}
答案 2 :(得分:0)