fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String regexSearch = "\\[[^\\]\\[|]*]";
  11. String regexReplacement = "*$0*";
  12. String inputString = "This is a User, [USER 1], and a second user [USER 2] not [USER | 3]";
  13. Pattern p = Pattern.compile(regexSearch);
  14. Matcher m = p.matcher(inputString);
  15. System.out.println(m.replaceAll(regexReplacement));
  16. }
  17. }
Success #stdin #stdout 0.12s 33680KB
stdin
Standard input is empty
stdout
This is a User, *[USER 1]*, and a second user *[USER 2]* not [USER | 3]