简体   繁体   中英

Java regular expressions and dollar sign

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.

Use

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

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

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(.)*";

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

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

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

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(.)*"; 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM