Algo de lo que me e dado cuenta cuando e dado platicas/talleres/cursos es que la gran mayoría de los desarrolladores, al menos de la región, no tienen ni puta idea acerca de que demonios son las expresiones regulares (Regex para los amigos).
Muchos recuerdan haber escuchado algo al respecto en su clase de autómatas en la universidad, pero como a todos, les paso de noche
.
Es difícil de entender como la gente puede pasarse sentado en su vida profesional sin saber Regex, escribiendo cientos de lineas de código resolviendo problemas que con unos cuantos caracteres de Regex queda solucionado.
Si aun no sabes Regex, ¡deja de leer esto inmediatamente y ponte a buscar información al respecto!
Para los escépticos, un pequeño ejemplo:
Imagina que quieres validar que un teléfono siga el formato (900) 9-00-00-00, la forma común de hacerlo seria algo cómo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | bool isValidPhoneNumber(string input) { //Validate (900) 9-00-00-00 if (input.Length != 16) return false; if (!input.StartsWith("(")) return false; if (input[4] != ')') return false; int number; if (!int.TryParse(input.Substring(1, 3), out number)) return false; if (input[1] == '0' || !char.IsNumber(input[1])) return false; if (input[5] != ' ') return false; string[] numbers = input.Substring(6, 10).Split ('-'); if (numbers.Length != 4) return false; foreach (string subpart in numbers) if (!int.TryParse(subpart, out number)) return false; if (numbers[0][0] == '0') return false; return true; } |
Es tedioso de hacer, resulta fácil dejar errores en el, sin mencionar que es completamente horrible y resulta difícil de entender, y precisamente este es uno de los momentos en que Regex es útil:
1 2 3 4 5 6 7 8 | bool isValidPhoneNumber(string input) { //Validate (900) 9-00-00-00 string expression = @"^\([1-9]\d{2}\)\ [1-9](\-\d{2}){3}$"; System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(expression); return regex.IsMatch(input); } |
Si bien la sintaxis puede resultar confusa al inicio, basta un poco de practica para acostumbrarse a ella y puede llegar a ahorrarte toneladas de trabajo, especialmente si la usas para buscar patrones de texto en ves de solamente validación.
Confíen en mi, si no las conocen, ¡Aprendan!.
P.S: No me hago responsable de cualquier mal uso que puedan hacer de ellas
Excelente tip, de hecho hasta hace unos dias que necesitaba hacer algo apenas supe de la existencia de las regex, voy a estudiar bien la sintaxis.
Si, y lo mejor de todo que la sintaxis es básicamente lo mismo en donde quiera que lo uses, ya sea Java, .NET, Perl, Python, Ruby, Javascript, etc.
Un Must-Have en todo baticinturon de un desarrollador.
Aw, this was an incredibly good post. Spending some time and actual effort to create a very good article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.
Hi! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My blog addresses a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you might be interested feel free to shoot me an email. I look forward to hearing from you! Terrific blog by the way!
I think that everything said was actually very logical. But, what about this? suppose you added a little content? I am not suggesting your content is not good., however what if you added something to maybe grab a person’s attention? I mean ¿No sabes Regex? ¡Aprende! » Historias de un Geek is a little plain. You might look at Yahoo’s home page and note how they write news titles to grab viewers to click. You might add a related video or a related picture or two to get readers interested about what you’ve written. Just my opinion, it would bring your blog a little bit more interesting.
Welches sind die Ersten Dementia Symptome noch dazu ist Geistiger Verfall Heilbar qualifiziert selbige hinauf solcher Netzauftritt etliche. http://berniewest821937.tumblr.com