File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes 'PatternSyntaxException' when attempting to use 'split' with a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Watch " New topic
Author

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

Jeremy Watts
Greenhorn

Joined: Jun 23, 2011
Posts: 18
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

Joined: Jun 19, 2010
Posts: 825
    
    5

You should escape the '+' character, since it's reserved (has a special meaning) with regular expressions. Try .split("\\+").


The quieter you are, the more you are able to hear.
Jeremy Watts
Greenhorn

Joined: Jun 23, 2011
Posts: 18
Hi, yes that worked, thanks. Does the double slash '\\' mean "treat what's coming next as a String" ?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

It means - treat '+' as a regular character, not as regular expression quantifier.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'PatternSyntaxException' when attempting to use 'split' with a String