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.
Thank you Steve!My condolences to his family and friends
I just like the valuable information you supply on your articles. I’ll bookmark your weblog and check once more right here frequently. I am slightly certain I’ll learn many new stuff right right here! Best of luck for the next!
Hi, Neat post. There is a problem with your web site in internet explorer, would check this… IE still is the market leader and a large portion of people will miss your wonderful writing due to this problem.
I will definately post a link to this post on my website. I’m sure my subscribers will find this post really fantastic.
One thing I would like to reply to is that weightloss program fast can be achieved by the proper diet and exercise. Your size not only affects appearance, but also the general quality of life. Self-esteem, major depression, health risks, plus physical skills are impacted in an increase in weight. It is possible to do everything right but still gain. Should this happen, a medical problem may be the root cause. While too much food and not enough body exercise are usually guilty, common health concerns and traditionally used prescriptions can easily greatly add to size. I am grateful for your post in this article.
I dont get my way on your website very quite often, but I must. You are a great writer and possess made me realise I would visit your site on a regular basis.
Hey! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no data backup. Do you have any methods to prevent hackers?