fork download
  1. // https://es.stackoverflow.com/q/69597/127
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String texto = "kdya";
  10. String regex = "^[kd]";
  11. Pattern pat = Pattern.compile(regex);
  12. Matcher mat = pat.matcher(texto);
  13.  
  14. if (mat.find())
  15. {
  16. System.out.println("El texto '" + texto + "' empieza con " + mat.group());
  17. }
  18. else
  19. {
  20. System.out.println("No coincide");
  21. }
  22. }
  23. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
El texto 'kdya' empieza con k