我们可以像这样用正则表达式拆分
一行:
String regex = "[\\p{P} ]+";
String[] split = line.split(regex);
但我想知道是否有办法将正则表达式缓存到 Pattern
中:
private static final Pattern PATTERN = Pattern.compile(regex);
然后使用Pattern
split
,达到这个程度:
String[] split = line.split(PATTERN);
最佳答案
当然,不是 String.split
, 但带有 Pattern.split
你可以:
String[] parts = PATTERN.split(line);
But I was wondering if there is a way to cache the regex into a Pattern:
您真正的意思是,用一个可以重复使用的已编译正则表达式拆分。
关于Java 缓存 String.split 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33484592/