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.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String[] strings = {
  12. "abc;erga_sd,cde;dfgef,g;4",
  13. "g;4,abc;dsfaweg",
  14. "cde;df_ger",
  15. "g;4",
  16. "abc;dsfg,dfvser"
  17. };
  18.  
  19. String regex = "^(?:(?:abc|cde);[a-z_]+|g;4)(?:,(?:(?:abc|cde);[a-z_]+|g;4))*$";
  20. Pattern pattern = Pattern.compile(regex);
  21.  
  22. for (String s : strings) {
  23. Matcher matcher = pattern.matcher(s);
  24. if (matcher.matches()) {
  25. System.out.printf("Match for %s%n", s);
  26. } else {
  27. System.out.printf("No match for %s%n", s);
  28. }
  29. }
  30. }
  31. }
Success #stdin #stdout 0.08s 48936KB
stdin
Standard input is empty
stdout
Match for abc;erga_sd,cde;dfgef,g;4
Match for g;4,abc;dsfaweg
Match for cde;df_ger
Match for g;4
No match for abc;dsfg,dfvser