Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'PatternSyntaxException' when attempting to use 'split' with a String

 
Jeremy Watts
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the string, "4563+943", and then when i attempt "4563+943".split("+"), I get the 'PatternSyntaxException' message. What is wrong here?

Thanks
Jeremy
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should escape the '+' character, since it's reserved (has a special meaning) with regular expressions. Try .split("\\+").
 
Jeremy Watts
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, yes that worked, thanks. Does the double slash '\\' mean "treat what's coming next as a String" ?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means - treat '+' as a regular character, not as regular expression quantifier.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it's not a "double slash". It's a single backslash character. It just looks like two characters because of the escaping rules for String literals.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic