|
| 1 | +/* |
| 2 | + * #16 EXPRESIONES REGULARES |
| 3 | +*/ |
| 4 | +import java.util.regex.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + public static void main(String[] args) { |
| 8 | + String texto = "Este texto contiene 5 numeros del 1 al 5: 1,2,3,4,5"; |
| 9 | + String regexNumeros = "\\d+"; |
| 10 | + Pattern pattern = Pattern.compile(regexNumeros); |
| 11 | + Matcher matcher = pattern.matcher(texto); |
| 12 | + while (matcher.find()) { |
| 13 | + System.out.println(matcher.group()); |
| 14 | + } |
| 15 | + System.out.println("------------------"); |
| 16 | + /* |
| 17 | + * DIFICULTAD EXTRA |
| 18 | + */ |
| 19 | + |
| 20 | + String emailCorrecto = "[email protected]"; |
| 21 | + String emailIncorrecto = "abcmail.com"; |
| 22 | + String regexEmail = "^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$"; |
| 23 | + |
| 24 | + System.out.println(emailCorrecto + " es un email valido? " + emailCorrecto.matches(regexEmail)); |
| 25 | + System.out.println(emailIncorrecto + " es un email valido? " + emailIncorrecto.matches(regexEmail)); |
| 26 | + |
| 27 | + String numeroCorrecto = "+51 987654321"; |
| 28 | + String numeroIncorrecto = "987654321"; |
| 29 | + String regexNumero = "^\\+[0-9]{2}\\s[0-9]{9}$"; |
| 30 | + |
| 31 | + System.out.println(numeroCorrecto + " es un numero valido? " + numeroCorrecto.matches(regexNumero)); |
| 32 | + System.out.println(numeroIncorrecto + " es un numero valido? " + numeroIncorrecto.matches(regexNumero)); |
| 33 | + |
| 34 | + String urlCorrecta = "https://retosdeprogramacion.com"; |
| 35 | + String urlIncorrecta = "retosdeprogramacion.com/"; |
| 36 | + String regexUrl = "^https?://(www\\.)?[a-zA-Z0-9-]+\\.[a-zA-Z]{2,}$"; |
| 37 | + |
| 38 | + System.out.println(urlCorrecta + " es una url valida? " + urlCorrecta.matches(regexUrl)); |
| 39 | + System.out.println(urlIncorrecta + " es una url valida? " + urlIncorrecta.matches(regexUrl)); |
| 40 | + } |
| 41 | +} |
0 commit comments