fork download
  1. //¿Como hacer un regex para validar que los dos elementos consecutivos no sean signos aritmeticos?
  2. // https://es.stackoverflow.com/q/110445/127
  3.  
  4. import java.util.regex.Pattern;
  5. import java.util.regex.Matcher;
  6.  
  7.  
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String allTexto = "5+4+4+3+2++*";
  13.  
  14. String regex = "[-+*/]{2}";
  15. Pattern pattern = Pattern.compile(regex);
  16. Matcher matcher = pattern.matcher(allTexto);
  17.  
  18. if (matcher.find()) {
  19. System.out.println("Tiene más de un operador consecutivo. '" + matcher.group() + "' aparece en la posición " + matcher.start());
  20. } else {
  21. System.out.println("No tiene operadores consecutivos");
  22. }
  23. }
  24. }
Success #stdin #stdout 0.06s 2841600KB
stdin
Standard input is empty
stdout
Tiene más de un operador consecutivo. '++' aparece en la posición 9