jueves, 26 de junio de 2014

Ejemplos de expresiones regulares

Regex Syntax

Characters


 DescriptionExample
  matchDoesn't match
.Any characterX[everything matches]
[abcde]A character of the set {abcde}*aA or f or #
[^abcde]A character which is not in the set {abcde}*A or gc
[a-z0-9]A character in the sets {a,b,c...z} {0,1,2...9}*bB or #
\w[a-zA-Z0-9_] (literal or digit or underscore)B or 9 or _# or " "(space)
\W[^a-zA-Z0-9_] (not literal nor digit nor underscore)# or " " (space)B or 9 _
\d[0-9] (digit)8A or #
\D[^0-9] (not a digit)A or #8
\sSpace" "A or 8 or #
\SNo spaceA or 8 or #" "
\xdddd is a hexadecimal number. Matches the character with ascii code dd.\x41 matches A \x40 matches @ 
\udddddddd is a hexadecimal character. Matches the character with unicode code dddd.\u0013 matches # \u0030 matches @ 
* Metacharacters Inside Character Classes:

Validar direccion email:
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Validar direccion email (permite email vacio):
^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)??\s*$
URL (protocolo, dominio, puerto, archivo, parametros):
\b(?i:https?|ftp)(://([\w.]+(:\d{1,4})?)(/[\w+&@#/%=~_!:,.;]*))?(\?[\w+&@#/%=~_|!:,.;]*)?
Valida IP: from 0.0.0.0 a 255.255.255.255 (no capture)
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\b
Path (drive, folder, filename):
\b(?i:[a-z]):\\([^/:*?"<>]*\\)?([^\\/:*?"<>]*)
Valida caracteres sean numeros, letras y subrayado:
^[\w]*$

Fuente

No hay comentarios:

Publicar un comentario