2

In java Regex i want to replace all the special characters to escape sequence.how can i do. Example ::

//special chars ex "dd[u]i.* " to "dd//[u//]i//.//*"
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105

2 Answers2

1

To escape all special regexp control characters this method can be used:

Matcher.quoteReplacement(String s)

It returns a regular expression that matches exact s.

This comes from the javadoc:

Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

wumpz
  • 8,257
  • 3
  • 30
  • 25
0

To split or treat special characters in java pattern as normal one. You have to backshlas it. \\.; \\* it might be treat now as '.' and '*'.

RMachnik
  • 3,598
  • 1
  • 34
  • 51