Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PatternSyntaxException

 
Jacob Sonia
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this program, it throws a patternsyntaxexception, not sure why
 
Stephan van Hulst
Bartender
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to replace? "\\" is not a valid regex.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"\\" 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic