fork download
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. class Replace {
  6. public static void main(String[] args) {
  7. String s= ":20:9405601140\n:2D::11298666\n:28C:20/1";
  8.  
  9. Scanner scanner = new Scanner(s);
  10. Pattern pattern = Pattern.compile("(:(\\w+)[:\\|]+)(.*)");
  11.  
  12. while (scanner.hasNextLine()) {
  13. String input = scanner.nextLine();
  14. Matcher matcher = pattern.matcher(input);
  15. if (matcher.matches()) {
  16. if (matcher.group(1).startsWith(":20")) {
  17. System.out.println(matcher.group(1) + "1234");
  18. }
  19. // more if statements or other rules
  20. else {
  21. // no rule
  22. System.out.println(input);
  23. }
  24. }
  25. else {
  26. System.out.println(input);
  27. }
  28. }
  29.  
  30. }
  31. }
  32.  
Success #stdin #stdout 0.09s 380736KB
stdin
Standard input is empty
stdout
:20:1234
:2D::11298666
:28C:20/1