2
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
 {
       int turns;
     Scanner scan=new Scanner(System.in);
     turns=scan.nextInt();
     while(turns-->0)
     {
       String pattern=scan.next();
       String text=scan.next();
       System.out.println(regex(pattern,text));

      }
 }//end of main method
 static int regex(String pattern,String text)
 {
     if(pattern.startsWith("^"))
       {
           if(text.startsWith(pattern.replace("^","")))
           return 1;
       }
       else if(pattern.endsWith("$"))
       {
           if(text.endsWith(pattern.replace("$","")))
           return 1;
       }
       else
       {
          if(text.contains(pattern))
          return 1; 
       }
       return 0;
 }
}

Input: 2 or$ hodor or$ arya

Output: 1 0

In this program i am scanning two parameters(String) in which first one is pattern and second one is text in which i have to find pattern. Method should return 1 if pattern matched else return 0. While using replace it is working fine but when i replace replace() to replaceAll() it is not working properly as expected. How can i make replaceAll() work in this program.

4 Answers4

6

Because replaceAll expects a string defining a regular expression, and $ means "end of line" in regular expressions. From the link:

public String replaceAll(String regex,
                         String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.

You need to escape it with a backslash (which also has to be escaped, in the string literal):

if(text.endsWith(pattern.replaceAll("\\$","")))

For complex strings that you want to replace verbatim, Pattern.quote is useful:

if(text.endsWith(pattern.replaceAll(Pattern.quote("$"),"")))

You don't need it here because your replacement is "", but if your replacement may have special characters in it (like backslashes or dollar signs), use Matcher.quoteReplacement on the replacement string as well.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 2
    @OldCurmudgeon And in replacement part [Matcher,quoteReplacement()](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#quoteReplacement-java.lang.String-) since `$` and ``\`` are also special there (`$x` to access match from group `x`, and ``\`` so we can escape `$`). – Pshemo Aug 19 '16 at 08:16
  • @OldCurmudgeon: Ah yes, thanks, Java actually has that useful method. I keep waiting for TC-39 to add it to JavaScript. :-| – T.J. Crowder Aug 19 '16 at 08:19
3

$ is a scpecial character in regex (EOL). You have to escape it

pattern.replaceAll("\\$","")
Jens
  • 67,715
  • 15
  • 98
  • 113
1

Despite the similar name, these are two very different methods.

replace replaces substrings with other substrings (*).

replaceAll uses regular expression matching, and $ is a special control character there (meaning "end of string/line").

You should not be using replaceAll here, but if you must, you have to quote the $:

 pattern.replaceAll(Pattern.quote("$"),"")

(*) to make things more confusing, replace also replaces all occurances, so the only difference in the method names does not all describe the difference in function.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
0

Introducing another level of complexity by replacing $ by \$.

"$ABC$AB".replaceAll(Matcher.quoteReplacement("$"), Matcher.quoteReplacement("\\\\$"))
// Output - \\$ABC\\$AB

This worked for me.

For the issue reported here,

"$ABC$AB".replaceAll(Matcher.quoteReplacement("$"), "")

should work.