Fri*_*rdt 31 java regex split guava
我最近利用了前瞻性正则表达式的功能来分割字符串:
"abc8".split("(?=\\d)|\\W")
Run Code Online (Sandbox Code Playgroud)
如果打印到控制台,则此表达式返回:
[abc, 8]
Run Code Online (Sandbox Code Playgroud)
对此结果非常满意,我想将其转移到Guava进行进一步开发,如下所示:
Splitter.onPattern("(?=\\d)|\\W").split("abc8")
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是输出变为:
[abc]
Run Code Online (Sandbox Code Playgroud)
为什么?
Jef*_*rey 15
你发现了一个错误!
System.out.println(s.split("abc82")); // [abc, 8]
System.out.println(s.split("abc8")); // [abc]
Run Code Online (Sandbox Code Playgroud)
这是Splitter用于实际拆分Strings(Splitter.SplittingIterator::computeNext)的方法:
@Override
protected String computeNext() {
/*
* The returned string will be from the end of the last match to the
* beginning of the next one. nextStart is the start position of the
* returned substring, while offset is the place to start looking for a
* separator.
*/
int nextStart = offset;
while (offset != -1) {
int start = nextStart;
int end;
int separatorPosition = separatorStart(offset);
if (separatorPosition == -1) {
end = toSplit.length();
offset = -1;
} else {
end = separatorPosition;
offset = separatorEnd(separatorPosition);
}
if (offset == nextStart) {
/*
* This occurs when some pattern has an empty match, even if it
* doesn't match the empty string -- for example, if it requires
* lookahead or the like. The offset must be increased to look for
* separators beyond this point, without changing the start position
* of the next returned substring -- so nextStart stays the same.
*/
offset++;
if (offset >= toSplit.length()) {
offset = -1;
}
continue;
}
while (start < end && trimmer.matches(toSplit.charAt(start))) {
start++;
}
while (end > start && trimmer.matches(toSplit.charAt(end - 1))) {
end--;
}
if (omitEmptyStrings && start == end) {
// Don't include the (unused) separator in next split string.
nextStart = offset;
continue;
}
if (limit == 1) {
// The limit has been reached, return the rest of the string as the
// final item. This is tested after empty string removal so that
// empty strings do not count towards the limit.
end = toSplit.length();
offset = -1;
// Since we may have changed the end, we need to trim it again.
while (end > start && trimmer.matches(toSplit.charAt(end - 1))) {
end--;
}
} else {
limit--;
}
return toSplit.subSequence(start, end).toString();
}
return endOfData();
}
Run Code Online (Sandbox Code Playgroud)
感兴趣的领域是:
if (offset == nextStart) {
/*
* This occurs when some pattern has an empty match, even if it
* doesn't match the empty string -- for example, if it requires
* lookahead or the like. The offset must be increased to look for
* separators beyond this point, without changing the start position
* of the next returned substring -- so nextStart stays the same.
*/
offset++;
if (offset >= toSplit.length()) {
offset = -1;
}
continue;
}
Run Code Online (Sandbox Code Playgroud)
除非在a结束时发生空匹配,否则这种逻辑很有效String.如果空匹配确实发生在a的结尾String,它将最终跳过该字符.这个部分应该是什么样的(注意>=- > >):
if (offset == nextStart) {
/*
* This occurs when some pattern has an empty match, even if it
* doesn't match the empty string -- for example, if it requires
* lookahead or the like. The offset must be increased to look for
* separators beyond this point, without changing the start position
* of the next returned substring -- so nextStart stays the same.
*/
offset++;
if (offset > toSplit.length()) {
offset = -1;
}
continue;
}
Run Code Online (Sandbox Code Playgroud)
Splitter当模式匹配空字符串时,Guava 似乎有一个bug.如果您尝试创建Matcher并打印出匹配的内容:
Pattern pattern = Pattern.compile("(?=\\d)|\\W");
Matcher matcher = pattern.matcher("abc8");
while (matcher.find()) {
System.out.println(matcher.start() + "," + matcher.end());
}
Run Code Online (Sandbox Code Playgroud)
你得到输出3,3,使它看起来像匹配8.因此它只是在那里分裂abc.
您可以使用eg Pattern#split(String)似乎给出正确的输出:
Pattern.compile("(?=\\d)|\\W").split("abc8")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1616 次 |
| 最近记录: |