本文介绍了番石榴:Splitter并考虑逃离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对番石榴的分裂可能性感兴趣:

I am interested in the Splitting possibility of Guava:

Splitter.on("|").split("foo|bar|baz");
// => "foo", "bar", "baz"

这是正常的。

如果我想拆分|现在怎么办?但不在[和]之间:

What now if I want to split on "|" but not between "[" and "]":

Splitter.on(something).split("foo|ba[r|ba]z");
// => "foo", "ba[r|ba]z"

根据我的理解,它不是可以在番石榴中定义这个东西。

From what I understood, it is not possible to define this "something" in Guava.

我发现了这个:
。这是否相关?

I found this:Issue 799: Add google escape library to Guava. Is this related ?

推荐答案

处理此问题的正确方法是创建解析器。现在真的很简单,只需使用解析器组合器,例如JParsec。你会得到这样的结果:

The proper way to deal with this is to make a parser. It's really easy nowadays, just use a parser combinator, such as JParsec. You'll get something like this:

class ParserFactory {

    Parser escapedSequence() {
        return Parsers.between(Scanners.string("["),
            Scanners.anyCharacterButNot("]"), Scanners.string("]"));
    }

    Parser chunk() {
        return Parsers.or(escapedSequence(), Scanners.anyCharacterButNot("|"));
    }

    Parsers wholeThing() {
        return Parsers.separatedBy(chunk().plus(), Scanners.string("|"));
    }
}

这篇关于番石榴:Splitter并考虑逃离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:49