|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.regex.Matcher; |
| 4 | +import java.util.regex.Pattern; |
| 5 | + |
| 6 | +public class Josegs95 { |
| 7 | + public static void main(String[] args) { |
| 8 | + //Ejercicio |
| 9 | + Pattern pattern = Pattern.compile("\\d"); |
| 10 | + Matcher matcher = pattern.matcher("En la calle ví 2 coches, 2 perros, 1 señora y 6 farolas"); |
| 11 | + while(matcher.find()){ |
| 12 | + System.out.println("Match: " + matcher.group()); |
| 13 | + } |
| 14 | + |
| 15 | + //Reto |
| 16 | + System.out.println("\n"); |
| 17 | + retoFinal(); |
| 18 | + } |
| 19 | + |
| 20 | + public static void retoFinal(){ |
| 21 | + /*Email |
| 22 | + He decidido que los emails son validos si tienen: |
| 23 | + Cadena(1-n) + '.'||'-'||'_'(opcional) + Cadena(1-n) |
| 24 | + @ |
| 25 | + Cadena(1-n) + '-'(opcional) + '.' + Cadena(2-3) |
| 26 | + */ |
| 27 | + List<String> emailList = getEmailExamples(); |
| 28 | + |
| 29 | + String emailPattern = "^\\w+([.\\-_]?\\w+)*@\\w+(\\-\\w+)?\\.\\w{2,3}$"; |
| 30 | + |
| 31 | + for (String email : emailList){ |
| 32 | + System.out.println("\""+ email + "\" es correcto: " + Pattern.matches(emailPattern, email)); |
| 33 | + } |
| 34 | + |
| 35 | + //Número de telefono |
| 36 | + /*He decidido que los número de teléfonos son validos si tienen: |
| 37 | + 9 dígitos o |
| 38 | + '+' + 11 dígitos |
| 39 | + */ |
| 40 | + List<String> phoneList = getPhoneExamples(); |
| 41 | + |
| 42 | + String phonePattern = "^\\d{9}|\\+\\d{11}$"; |
| 43 | + |
| 44 | + for (String phoneNumber : phoneList){ |
| 45 | + System.out.println("\""+ phoneNumber + "\" es correcto: " + Pattern.matches(phonePattern, phoneNumber)); |
| 46 | + } |
| 47 | + |
| 48 | + //URL |
| 49 | + /*He decidido que las URL son validas si tienen: |
| 50 | + 'http'|'https''://'(opcional) + (Cadena(1-n) + '.')(opcional) + |
| 51 | + Cadena(1-n) + ('-' + Cadena(1-n))(opcional) + '.' + Cadena(2-3) |
| 52 | + */ |
| 53 | + List<String> urlList = getURLExamples(); |
| 54 | + |
| 55 | + //String urlPattern = "^((http|https)?://)?(\\w+\\.)?\\w+\\.\\w{2,3}"; |
| 56 | + String urlPattern = "^((http|https)?://)?(\\w+\\.)?[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*\\.\\w{2,3}"; |
| 57 | + |
| 58 | + for (String url : urlList){ |
| 59 | + System.out.println("\""+ url + "\" es correcta: " + Pattern.matches(urlPattern, url)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static List<String> getEmailExamples(){ |
| 64 | + List<String> emailList = new ArrayList<>(); |
| 65 | + emailList. add( "[email protected]"); |
| 66 | + emailList. add( "[email protected]"); |
| 67 | + emailList. add( "[email protected]"); |
| 68 | + emailList. add( "[email protected]"); |
| 69 | + emailList. add( "[email protected]"); |
| 70 | + |
| 71 | + emailList. add( "[email protected]"); |
| 72 | + emailList. add( "[email protected]"); |
| 73 | + emailList. add( "[email protected]"); |
| 74 | + emailList. add( "[email protected]"); |
| 75 | + emailList.add("juan.carlos@gmailcom."); |
| 76 | + emailList. add( "[email protected]"); |
| 77 | + emailList.add("juan.carlos@gmail-com"); |
| 78 | + emailList. add( "[email protected]"); |
| 79 | + |
| 80 | + return emailList; |
| 81 | + } |
| 82 | + |
| 83 | + private static List<String> getPhoneExamples(){ |
| 84 | + List<String> phoneList = new ArrayList<>(); |
| 85 | + phoneList.add("666554433"); |
| 86 | + phoneList.add("+34666554433"); |
| 87 | + |
| 88 | + phoneList.add("666554433808"); |
| 89 | + phoneList.add("66+6554433808"); |
| 90 | + phoneList.add("66655"); |
| 91 | + phoneList.add("66655plko"); |
| 92 | + |
| 93 | + return phoneList; |
| 94 | + } |
| 95 | + |
| 96 | + private static List<String> getURLExamples(){ |
| 97 | + List<String> urlList = new ArrayList<>(); |
| 98 | + urlList.add("https://www.google.es"); |
| 99 | + urlList.add("www.google.es"); |
| 100 | + urlList.add("videos.google.es"); |
| 101 | + urlList.add("google.es"); |
| 102 | + urlList.add("www.google-new.es"); |
| 103 | + |
| 104 | + urlList.add("hptts://www.google.es"); |
| 105 | + urlList.add(".google.es"); |
| 106 | + urlList.add("google."); |
| 107 | + urlList.add("www.goo_gle.es"); |
| 108 | + urlList.add("www.google-.es"); |
| 109 | + urlList.add("www.goo gle.es"); |
| 110 | + urlList.add("www.google.españa"); |
| 111 | + |
| 112 | + return urlList; |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments