I want to create a Splitter
(guava) and split a string based on a regex pattern. The split should take place either on [
or on ..
character. And what if I want to include eg 10 characters for a split? Would I have to separate each of them by |
in the regex?
But the following does not work: Pattern.compile("[|..")
Test: test[me..once more
should result in
test
me
once more
You can use the following regex to split:
(?:\[|\.\.)
In Java, you will have to escape the \\
s.
String str = "test[me..once more";
String pattern = "(?:\\[|\\.\\.)";
String[] parts = str.split(pattern);
Your pattern does not work because [
and .
are control characters in regexes.
try escaping them like Pattern.compile("\\\\[|\\\\.\\\\.")
(remember, \\
also needs to be escaped)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.