Search code examples
javaregexstringstring-parsing

Regular Expression to replace comma separated words with question marks


I want to replace the following strings:

chrome, internet explorer, mozlla, opera

or

john, jack, jill, bill

with

?, ?, ?, ?

I need a regular expression for Java which can replace the above string. Find all words and replace with question marks


Solution

  • Something like this:

    String output = myString.replaceAll("(chrome|internet|explorer|mozlla|opera)", "?");
    

    [Edit] You are changing the question faster then I can answer.

    To replace any word:

    String output = myString.replaceAll("\b(\w+)\b", "?");
    

    \b - first or last character in the word
    \w - alphanumeric
    + - one or more repetitions