|
| 1 | +import java.util.List; |
| 2 | +import java.util.regex.Pattern; |
| 3 | + |
| 4 | +/** |
| 5 | + * #16 EXPRESIONES REGULARES |
| 6 | + * |
| 7 | + * @author martinbohorquez |
| 8 | + */ |
| 9 | +public class martinbohorquez { |
| 10 | + |
| 11 | + public static void main(String[] args) { |
| 12 | + String texto = "Achieve your goals in 2024 by setting 3 milestones per quarter, " + |
| 13 | + "and track your progress every 30 days consistently!"; |
| 14 | + String regex = "\\D+";// Split by non-digit characters |
| 15 | + Pattern pattern = Pattern.compile(regex); |
| 16 | + |
| 17 | + List<String> digits = pattern.splitAsStream(texto) |
| 18 | + .filter(s -> !s.isBlank())// Filter empty elements |
| 19 | + .toList(); |
| 20 | + |
| 21 | + System.out.println(digits); |
| 22 | + |
| 23 | + /* |
| 24 | + * DIFICULTAD EXTRA |
| 25 | + */ |
| 26 | + String email = "email"; |
| 27 | + match( email, "[email protected]"); |
| 28 | + match( email, "[email protected]"); |
| 29 | + match( email, "[email protected]"); |
| 30 | + match( email, "[email protected]"); |
| 31 | + match( email, "[email protected]"); |
| 32 | + String phoneNumber = "phoneNumber"; |
| 33 | + match(phoneNumber, "+51 999 345 654"); |
| 34 | + match(phoneNumber, "+(51) 999 345 654"); |
| 35 | + match(phoneNumber, "999345654"); |
| 36 | + match(phoneNumber, "+51 123 345"); |
| 37 | + match(phoneNumber, "32 999345654"); |
| 38 | + String url = "url"; |
| 39 | + match(url, "http://www.google.com?name=pepe"); |
| 40 | + match(url, "https://www.facebook.com.pe/user/id=1"); |
| 41 | + match(url, "m.tiktok.com/live/@mark"); |
| 42 | + match(url, "www.peru-champions.com.pe/portal/id=3"); |
| 43 | + match(url, "instagram.com.br/login?"); |
| 44 | + } |
| 45 | + |
| 46 | + private static void match(String tipo, String texto) { |
| 47 | + String regexEmail = "^([\\w.+-]+@[\\w.-]+)$"; |
| 48 | + String regexPhoneNumber = "^\\+?(\\(?\\d{1,3}\\)?)?(\\s\\d+)*|\\d{3,}(\\s\\d+)*$"; |
| 49 | + String regexUrl = "^(?:https?://)?(?:(?:www|m)\\.)?((?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,})([/?]+\\S*)?$"; |
| 50 | + |
| 51 | + switch (tipo) { |
| 52 | + case "email" -> |
| 53 | + System.out.printf("El correo electrónico '%s' es válido: %b", texto, texto.matches(regexEmail)); |
| 54 | + case "phoneNumber" -> |
| 55 | + System.out.printf("El número telefónico '%s' es válido: %b", texto, texto.matches(regexPhoneNumber)); |
| 56 | + case "url" -> System.out.printf("La URL '%s' es válida: %b", texto, texto.matches(regexUrl)); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
0 commit comments