[英]Java Caching String.split Regex
We can split
a line by a regex like so: 我们可以像这样用正则表达式split
一行:
String regex = "[\\p{P} ]+";
String[] split = line.split(regex);
But I was wondering if there is a way to cache the regex into a Pattern
: 但是我想知道是否有一种方法可以将正则表达式缓存到Pattern
:
private static final Pattern PATTERN = Pattern.compile(regex);
Then split
using the Pattern
, something to this extent: 然后使用Pattern
某种程度的split
:
String[] split = line.split(PATTERN);
Sure, not with String.split
, but with Pattern.split
you can: 当然,不是通过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: 但我想知道是否有办法将正则表达式缓存到模式中:
What you really meant was, split by a compiled regex that can be reused. 您真正的意思是,将其分解为可重用的已编译正则表达式 。
You can use: Pattern#split(String) method like this: 您可以使用: Pattern#split(String)方法,如下所示:
final Pattern p = Pattern.compile("[\\p{P} ]+");
Then use it: 然后使用它:
Strig[] arr = p.split(line);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.