fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String regex = "(?:\\G(?!^)|^(?=(?:[0-9a-fA-F]{2})*$))[0-9a-fA-F]{2}";
  12. String[] strings = {"aabb","_aabb","aa_bb", "aabb_"};
  13. Pattern pattern = Pattern.compile(regex);
  14. for (String s : strings) {
  15. System.out.println("Checking " + s);
  16. Matcher matcher = pattern.matcher(s);
  17. List<String> res = new ArrayList<>();
  18. while (matcher.find()) {
  19. res.add(matcher.group(0));
  20. }
  21. if (res.size() > 0) {
  22. System.out.println(res);
  23. } else {
  24. System.out.println("No match!");
  25. }
  26. }
  27. }
  28. }
Success #stdin #stdout 0.09s 27880KB
stdin
Standard input is empty
stdout
Checking aabb
[aa, bb]
Checking _aabb
No match!
Checking aa_bb
No match!
Checking aabb_
No match!