-2

I want to replace (words that starts with letter 'a' has one or more chars and ends with 'a') with sign '!' and print it, but nothing is working. Why?

class Test
{
    public static void main(String[] args)
    {
        String s = "aba accca azzza wwwwa";
        System.out.println(s = s.replaceAll("\ba.+?a\b", "!"));
    }
}

Current ouput: aba accca azzza wwwwa

Desired ouput: ! ! ! wwwwa

Denys_newbie
  • 1,140
  • 5
  • 15

1 Answers1

0

The backslash inside replaceAll() should be escaped like below:

public static void main(String args[]) throws IOException {
               String s = "aba accca azzza wwwwa";
               s = s.replaceAll("\\ba.+?a\\b", "!");
               System.out.println(s);
    }

output: ! ! ! wwwwa

Vignesh_A
  • 508
  • 1
  • 7
  • 19