Inicio > MySQL, PROGRAMACION > 5 Expresiones Regulares Muy Útiles para El Desarrollador Web

5 Expresiones Regulares Muy Útiles para El Desarrollador Web

Jueves, 9 de Agosto de 2007 Neozeratul Dejar un comentario Ir a comentarios

En Sentido Web han publicado 5 expresiones regulares que nos pueden ser útiles a todos los desarrolladores web y es que con una sola expresion regular bien implementada podemos ahorrarnos varias lineas de código.

Comprobar que una cadena solo contenga carateres alfanumericos y que la longitud este entre 3 y 16.

  1. /^[a-zA-Z0-9_]{3,16}$/

Ejemplo de Uso en PHP

  1. function validate_username( $username ) {

  2. if(preg_match(‘/^[a-zA-Z0-9_]{3,16}$/’, $_GET[‘username’])) {

  3. return true;

  4. }

  5. return false;

  6. }

Encontrar una etiqueta XHTML o XML

  • {<tag[^>]*>(.*?)</tag>}

Etiqueta XHTML o XML con atributos

  • {<tag[^>]*attribute\s*=\s*([”‘])value\\1[^>]*>(.*?)</tag>}

Verificar Dirección de Correo Electrónico (Email)

  1. function is_valid_email_address($email){

  2. $qtext = ‘[^\x0d\x22\x5c\x80-\xff]’;

  3. $dtext = ‘[^\x0d\x5b-\x5d\x80-\xff]’;

  4. $atom = ‘[^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c’.

  5. \x3e\x40\x5b-\x5d\x7f-\xff]+’;

  6. $quoted_pair = \x5c[\x00-\x7f]’;

  7. $domain_literal = \x5b($dtext|$quoted_pair)*\x5d”;

  8. $quoted_string = \x22($qtext|$quoted_pair)*\x22″;

  9. $domain_ref = $atom;

  10. $sub_domain = “($domain_ref|$domain_literal)”;

  11. $word = “($atom|$quoted_string)”;

  12. $domain = “$sub_domain(\x2e$sub_domain)*”;

  13. $local_part = “$word(\x2e$word)*”;

  14. $addr_spec = “$local_part\x40$domain”;

  15.  

  16. return preg_match(“!^$addr_spec$!”, $email) ? 1 : 0;

  17. }

Validar URL 

  1. {

  2. \b

  3. # Match the leading part (proto://hostname, or just hostname)

  4. (

  5. # http://, or https:// leading part

  6. (https?)://[-\w]+(\.\w[-\w]*)+

  7. |

  8. # or, try to find a hostname with more specific sub-expression

  9. (?i: [a-z0-9] (?:[-a-z0-9]*[a-z0-9])? \. )+ # sub domains

  10. # Now ending .com, etc. For these, require lowercase

  11. (?-i: com\b

  12. | edu\b

  13. | biz\b

  14. | gov\b

  15. | in(?:t|fo)\b # .int or .info

  16. | mil\b

  17. | net\b

  18. | org\b

  19. | [a-z][a-z]\.[a-z][a-z]\b # two-letter country code

  20. )

  21. )

  22.  

  23. # Allow an optional port number

  24. ( : \d+ )?

  25.  

  26. # The rest of the URL is optional, and begins with /

  27. (

  28. /

  29. # The rest are heuristics for what seems to work well

  30. [^.!,?;\’&lt;&gt;()[]{}sx7F-\xFF]*

  31. (

  32. [.!,?]+ [^.!,?;”\���&lt;&gt;()\[\]{\}s\x7F-\xFF]+

  33. )*

  34. )?

  35. }ix

Categories: MySQL, PROGRAMACION Tags:
  1. Sin comentarios aún.
  1. Sin trackbacks aún.