25

I have Java string:

String b = "/feedback/com.school.edu.domain.feedback.Review$0/feedbackId");

I also have generated pattern against which I want to match this string:

String pattern = "/feedback/com.school.edu.domain.feedback.Review$0(.)*";

When I say b.matches(pattern) it returns false. Now I know dollar sign is part of Java RegEx, but I don't know how should my pattern look like. I am assuming that $ in pattern needs to be replaced by some escape characters, but don't know how many. This $ sign is important to me as it helps me distinguish elements in list (numbers after dollar), and I can't go without it.

tshepang
  • 12,111
  • 21
  • 91
  • 136
azec-pdx
  • 4,790
  • 6
  • 56
  • 87
  • Just to be clear, `$` in a regex matches the end of line. Your regex will match a string whose first line ends with `Review` and the next line begins with the character `0`. – Amarghosh Oct 04 '10 at 10:21
  • 4
    @Amarghosh: I don't think so. This regex can *never* match because there must be a CR and/or LF between the end of line one and the first characters of line two. – Tim Pietzcker Oct 04 '10 at 11:55
  • 2
    @Tim Oops, you're right - I missed it. Since I can't edit the comment anymore, let me just ask people to __read Tim's correction to my first comment__ – Amarghosh Oct 04 '10 at 12:59

5 Answers5

61

Use

String escapedString = java.util.regex.Pattern.quote(myString)

to automatically escape all special regex characters in a given string.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
36

You need to escape $ in the regex with a back-slash (\), but as a back-slash is an escape character in strings you need to escape the back-slash itself.

You will need to escape any special regex char the same way, for example with ".".

String pattern = "/feedback/com\\.navteq\\.lcms\\.common\\.domain\\.poi\\.feedback\\.Review\\$0(.)*";
theon
  • 14,170
  • 5
  • 51
  • 74
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • Do you still need to escape them when it is a character class in Java. So if I want any character except a dot, dollar sign or percent it, this is ok right `[^.$%]`. No need to escape then.. – JGFMK Nov 12 '19 at 12:35
8

In Java regex both . and $ are special. You need to escape it with 2 backslashes, i.e..

"/feedback/com\\.navtag\\.etc\\.Review\\$0(.*)"

(1 backslash is for the Java string, and 1 is for the regex engine.)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
3

Escape the dollar with \

String pattern = 
  "/feedback/com.navteq.lcms.common.domain.poi.feedback.Review\\$0(.)*";

I advise you to escape . as well, . represent any character.

String pattern = 
  "/feedback/com\\.navteq\\.lcms\\.common\\.domain\\.poi\\.feedback\\.Review\\$0(.)*"; 
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
2

The ans by @Colin Hebert and edited by @theon is correct. The explanation is as follows. @azec-pdx

  1. It is a regex as a string literal (within double quotes).

  2. period (.) and dollar-sign ($) are special regex characters (metacharacters).

  3. To make the regex engine interpret them as normal regex characters period(.) and dollar-sign ($), you need to prefix a single backslash to each. The single backslash ( itself a special regex character) quotes the character following it and thus escaping it.

  4. Since the given regex is a string literal, another backslash is required to be prefixed to each to avoid confusion with the usual visible-ASCII escapes(character, string and Unicode escapes in string literals) and thus avoid compiler error.

  5. Even if you use within a string literal any special regex construct that has been defined as an escape sequence, it needs to be prefixed with another backslash to avoid compiler error.For example, the special regex construct (an escape sequence) \b (word boundary) of regex would clash with \b(backspace) of the usual visible-ASCII escape(character escape). Thus another backslash is prefixed to avoid the clash and then \\b would be read by regex as word boundary.

  6. To be always safe, all single backslash escapes (quotes) within string literals are prefixed with another backslash. For example, the string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.

  7. The last period (.)* is supposed to be interpreted as special regex character and thus it needs no quoting by a backslash, let alone prefixing a second one.

rps
  • 331
  • 3
  • 8