开发者

Groovy: Escaping an arbitrary (unknown) regular expression

I want to split a string via String.split(k) where k is an arbitrary string (read from intput). The problem is that k can be any string and thus may include regular expression operators, such as: "*[".

In such cases the split method fires an exception since the regular expression is not well formed.

What I am looking for is a way to escape any given k such that the resulting string can be safely passed 开发者_运维技巧on split().

Any thoughts?


You can use Pattern.quote() to escape regex patterns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜