-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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.