fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String s = "\\x12\\x13\\x14\\x00\\xff\\xff";
  10.  
  11. // Extract valid blocks
  12. Pattern pattern = Pattern.compile("\\\\x[0-9A-Fa-f]{2}");
  13. Matcher matcher = pattern.matcher(s);
  14. List<String> res = new ArrayList<>();
  15. while (matcher.find()){
  16. res.add(matcher.group(0));
  17. }
  18. System.out.println(res);
  19.  
  20. // Check if a string consists of valid "blocks" only
  21. boolean isValid = s.matches("(?i)(?:\\\\x[a-f0-9]{2})+");
  22. System.out.println(isValid);
  23.  
  24. // More checks
  25. System.out.println("\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17".matches("(?i)(?:\\\\x[a-f0-9]{2})+"));
  26. System.out.println("\\x10\\x11\\x12\\x13\\x14\\x15\\16\\x1".matches("(?i)(?:\\\\x[a-f0-9]{2})+"));
  27. }
  28. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
[\x12, \x13, \x14, \x00, \xff, \xff]
true
true
false