This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes PatternSyntaxException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PatternSyntaxException" Watch "PatternSyntaxException" New topic


Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
I wrote this program, it throws a patternsyntaxexception, not sure why
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 3647

What are you trying to replace? "\\" is not a valid regex.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19723

"\\" is a String with one character, \. That happens to be an escape character in regular expressions as well. That means that you will need to escape it in your regex. That turns the regex into \\, so the String becomes "\\\\".

Or you simply use String.replace(). Since Java 5.0 or 6 it has been overloaded to take any CharSequence, which is an interface implemented by String. It behaves a bit like replaceAll except it takes a literal String, not a regex.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: PatternSyntaxException