fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception {
  9. final String regex = "(?:(?:|(\\|))f-150|keep|those):|(?:^|\\|)\\w-\\d{3}:\\w{2}";
  10. final String string = "f-150:aa|f-150:cc|g-210:dd|f-150:aa";
  11. final String subst = "$1";
  12.  
  13. final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  14. final Matcher matcher = pattern.matcher(string);
  15.  
  16. // The substituted value will be contained in the result variable
  17. final String result = matcher.replaceAll(subst);
  18.  
  19. System.out.println("Substitution result: " + result);
  20. }
  21. }
Success #stdin #stdout 0.08s 27892KB
stdin
Standard input is empty
stdout
Substitution result: aa|cc|aa