Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regular expressions and dollar sign

Tags:

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.

like image 869
azec-pdx Avatar asked Oct 04 '10 08:10

azec-pdx


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you escape the dollar sign in regex?

You can escape dollar signs with a backslash or with another dollar sign. So $, $$, and \$ all replace with a single dollar sign.

Is a dollar sign a special character in Java?

"Java does allow the dollar sign symbol $ to appear in an identifier, but these identifiers have a special meaning, so you should not use the $ symbol in your identifiers."

What is $1 regex Java?

$0 = the entire matched substring (corresponding to matcher. group()), $1 = the first parenthesized match subpattern (corresponding to matcher.


1 Answers

Use

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

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

like image 172
Tim Pietzcker Avatar answered Sep 21 '22 21:09

Tim Pietzcker