0

I'm trying to split an input by ".,:;()[]"'\/!? " chars and add the words to a list. I've tried .split("\\W+?") and .split("\\W"), but both of them are returning empty elements in the list.

Additionally, I've tried .split("\\W+"), which returns only words without any special characters that should go along with them (for instance, if one of the input words is "C#", it writes "C" in the list). Lastly, I've also tried to put all of the special chars above into the .split() method: .split("\\.,:;\\(\\)\\[]\"'\\\\/!\\? "), but this isn't splitting the input at all. Could anyone advise please?

1
  • 1
    You need a character class [] and you need to escape special characters (which are different for a character class than for a regex in general), so: split("[.,:;()\\[\\]\"'\\\\/!? ]+") Commented Apr 13, 2017 at 9:53

1 Answer 1

2

split() function accepts a regex.

This is not the regex you're looking for .split("\\.,:;\\(\\)\\[]\"'\\\\/!\\? ")

Try creating a character class like [.,:;()\[\]'\\\/!\?\s"] and add + to match one or more occurences.

I also suggest to change the character space with the generic \s who takes all the space variations like \t.

If you're sure about the list of characters you have selected as splitters, this should be your correct split with the correct Java string literal as @Andreas suggested:

.split("[.,:;()\\[\\]'\\\\\\/!\\?\\s\"]+")

BTW: I've found a particularly useful eclipse editor option which escapes the string when you're pasting them into the quotes. Go to Window/Preferences, under Java/Editor/Typing/, check the box next to Escape text when pasting into a string literal

Sign up to request clarification or add additional context in comments.

2 Comments

No need to escape ., (, ), and ? inside a character class.
Why do you have " and ) twice? And you should show how to do it as a Java string literal, since the escaping gets worse there.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.