8

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?

1 Answer 1

10

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.