0

Every time i try to split the string "hello*world" using s.split("*"); I get a PatternSyntaxException.

I have tried using s.split("\*"); but that gives me another error. Im sure this is something simple.

How do i stop this?

2 Answers 2

5

* is a meta-character in regular expressions used as a wildcard quantifier to match zero of more characters

Try using 2 backslash characters

s.split("\\*");
Sign up to request clarification or add additional context in comments.

2 Comments

I think you should include this: Meta-character <([{\^-=$!|]})?*+.>
Or use Pattern.quote() or the Matcher.quoteReplacement() equivalent.
3

The split method takes a regular expression as the argument, not a normal string. The * has special meaning in regular expressions. If you want to split on a literal *, you have to escape it with a backslash. But the backslash is also an escape character in Java string literals, so you have to escape the backslash too by using two backslashes:

s.split("\\*")

Comments

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.