1
+ using System . Text . RegularExpressions ;
2
+
3
+ class Program
4
+ {
5
+ static void Main ( string [ ] args )
6
+ {
7
+ #region RegEx
8
+ // Expresiones Regulares
9
+ string texto = "1Est3e T8ext0o tie5n4e n0um98er5467os q78u11e de7851b44e651n313 s51e35r 00ob5t3en5i1d6o0s5" ;
10
+ // El siguiente patrón busca números del 0 al 9
11
+ string pattern = "[0-9]" ;
12
+ /*
13
+ * Se inicializa un objeto de tipo Regex y se envía
14
+ * el patrón anterior en su constructor
15
+ */
16
+ var regex = new Regex ( pattern ) ;
17
+ /*
18
+ * El método Matches() devuelve una colección con
19
+ * todas las coincidencias encontradas en el texto
20
+ * especificado
21
+ */
22
+ var numeros = regex . Matches ( texto ) ;
23
+ Console . WriteLine ( $ "Numeros Encontrados en texto: { texto } ") ;
24
+ /*
25
+ * Recorremos la colección e imprimimos todos las
26
+ * coincidencias
27
+ */
28
+ foreach ( var numero in numeros )
29
+ Console . Write ( $ "{ numero } , ") ;
30
+ Console . WriteLine ( ) ;
31
+ /*
32
+ * La clase Regex contiene el método estático Replace()
33
+ * el cual reemplaza dentro de la cadena envíada el patrón
34
+ * que se indique por otro carácter
35
+ */
36
+ texto = Regex . Replace ( texto , pattern , "" ) ;
37
+ Console . WriteLine ( $ "Texto con números eliminados: { texto } ") ;
38
+ #endregion
39
+ // Ejercicio extra
40
+ Console . ReadLine ( ) ;
41
+ Console . Clear ( ) ;
42
+ bool salir = false ;
43
+ do
44
+ {
45
+ Menu ( ) ;
46
+ string option = Console . ReadLine ( ) ;
47
+ switch ( option . ToLower ( ) )
48
+ {
49
+ case "a" :
50
+ var responseEmail = ValidarEmail ( ) ;
51
+ if ( responseEmail . Item2 )
52
+ Console . WriteLine ( $ "El email { responseEmail . Item1 } es válido...") ;
53
+ else
54
+ Console . WriteLine ( $ "El email { responseEmail . Item1 } no es válido...") ;
55
+ break ;
56
+ case "b" :
57
+ var responseTelefono = ValidarNumero ( ) ;
58
+ if ( responseTelefono . Item2 )
59
+ Console . WriteLine ( $ "El número { responseTelefono . Item1 } es válido...") ;
60
+ else
61
+ Console . WriteLine ( $ "El número { responseTelefono . Item1 } no es válido...") ;
62
+ break ;
63
+ case "c" :
64
+ var responseURL = ValidarURL ( ) ;
65
+ if ( responseURL . Item2 )
66
+ Console . WriteLine ( $ "La URL { responseURL . Item1 } es válida...") ;
67
+ else
68
+ Console . WriteLine ( $ "La URL { responseURL . Item1 } no es válida...") ;
69
+ break ;
70
+ case "d" :
71
+ Console . Clear ( ) ;
72
+ Console . WriteLine ( "Hasta la próxima..." ) ;
73
+ salir = true ;
74
+ Thread . Sleep ( 1000 ) ;
75
+ break ;
76
+ default :
77
+ Console . Clear ( ) ;
78
+ Console . WriteLine ( "Opción no válida..." ) ;
79
+ break ;
80
+ }
81
+ }
82
+ while ( ! salir ) ;
83
+
84
+ }
85
+ static void Menu ( )
86
+ {
87
+ Console . WriteLine ( "---Sistema RegEx---" ) ;
88
+ Console . WriteLine ( "a.- Validar email" ) ;
89
+ Console . WriteLine ( "b.- Validar número telefónico" ) ;
90
+ Console . WriteLine ( "c.- Validar URL" ) ;
91
+ Console . WriteLine ( "d.- Salir" ) ;
92
+ Console . WriteLine ( "Selecciones la opción deseada..." ) ;
93
+ }
94
+ static ( string , bool ) ValidarEmail ( )
95
+ {
96
+ Console . Clear ( ) ;
97
+ Console . WriteLine ( "Ingresa email a validar" ) ;
98
+ string email = Console . ReadLine ( ) ;
99
+
100
+ if ( string . IsNullOrEmpty ( email ) )
101
+ return ( email , false ) ;
102
+
103
+ string pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$" ;
104
+ /*
105
+ * ^ -> Comenzar la búsqueda al inicio de la cadena
106
+ * [^@\s]+ -> Busca uno o más carácterers diferentes de @ o espacio en blanco
107
+ * @ -> Busca el carácter @
108
+ * \. -> Busca un único carácter de punto
109
+ * $ -> Finalizar la busqueda al final de la cadena
110
+ */
111
+ return Regex . IsMatch ( email , pattern ) ? ( email , true ) : ( email , false ) ;
112
+ }
113
+ static ( string , bool ) ValidarNumero ( )
114
+ {
115
+ Console . Clear ( ) ;
116
+ Console . WriteLine ( "Ingresa número teléfonico a validar" ) ;
117
+ string numero = Console . ReadLine ( ) ;
118
+ if ( string . IsNullOrEmpty ( numero ) )
119
+ return ( numero , false ) ;
120
+ string pattern = @"^(\+?\s?\d{3}[\s.-]?\d{3}[\s.-]?\d{4}|\d{10})$" ;
121
+ /*
122
+ * ^ -> Comenzar la búsqueda al inicio de la cadena
123
+ * \+? -> Puede comenzar con un +
124
+ * \s -> Puede incluir un espacio en blanco
125
+ * \d{3} Debe incluir tres dígitos
126
+ * [\s.-]? -> Puede contener un espacio en blanco, un punto o un guión
127
+ * \d{4} -> Debe incluir cuatro dígitos
128
+ * |\d{10} O incluye dies dígitos
129
+ * $ -> Finalizar la busqueda al final de la cadena
130
+ */
131
+ return Regex . IsMatch ( numero , pattern ) ? ( numero , true ) : ( numero , false ) ;
132
+ }
133
+ static ( string , bool ) ValidarURL ( )
134
+ {
135
+ Console . Clear ( ) ;
136
+ Console . WriteLine ( "Ingresa URL a validar" ) ;
137
+ string url = Console . ReadLine ( ) ;
138
+ if ( string . IsNullOrEmpty ( url ) )
139
+ return ( url , false ) ;
140
+ string pattern = @"^(http+s?\:\/\/)?(www\.)?\w+\.\w+$" ;
141
+ /*
142
+ * ^ -> Comenzar la búsqueda al inicio de la cadena
143
+ * (http+s?\:\/\/)? -> Puede incluir http:// o https://
144
+ * (www\.)? -> Puede incluir www.
145
+ * \w+ -> Incluye uno o varios carácteres alfanuméricos
146
+ * \. Incluye un punto
147
+ * $ -> Finalizar la busqueda al final de la cadena
148
+ */
149
+
150
+ return Regex . IsMatch ( url , pattern ) ? ( url , true ) : ( url , false ) ;
151
+ }
152
+ }
0 commit comments