Expresiones Regulares (Chuletilla)
Las expresiones regulares son muy útiles en Unix por eso tener una chuletilla con ellas (o casi todas) siempre está bien. La chuleta o cheetsheet original la podéis encontrar en PDF o en PNG en esta web: http://www.addedbytes.com.
Posicionamiento ^ Start of line ** \A Start of string ** $ End of line ** \Z End of string ** \b Word boundary ** \B Not word boundary ** \< Start of word \> End of word
Tipos de caracteres \c Control character \s White space \S Not white space \d Digit \D Not digit \w Word \W Not word \xhh Hexadecimal character hh \Oxxx Octal character xxx
Tipos de caracteres (POSIX) [:upper:] Upper case letters [:lower:] Lower case letters [:alpha:] All letters [:alnum:] Digits and letters [:digit:] Digits [:xdigit:] Hexadecimal digits [:punct:] Punctuation [:blank:] Space and tab [:space:] Blank characters [:cntrl:] Control characters [:graph:] Printed characters [:print:] Printed characters and spaces [:word:] Digits, letters and underscore
Assertions ?= Lookahead assertion ** ?! Negative lookahead ** ?<= Lookbehind assertion ** ?!= Negative lookbehind ** ?** ?> Once-only Subexpression ?() Condition [if then] ?()| Condition [if then else] ?# Comment
Cuantificadores
* 0 or more **
*? 0 or more, ungreedy **
+ 1 or more **
+? 1 or more, ungreedy **
? 0 or 1 **
?? 0 or 1, ungreedy **
{3} Exactly 3 **
{3,} 3 or more **
{3,5} 3, 4 or 5 **
{3,5}? 3, 4 or 5, ungreedy **
Rangos (inclusives) . Any character except new line (\n) + (a|b) a or b ** (...) Group ** (?:...) Passive Group ** [abc] Range (a or b or c) ** [^abc] Not a or b or c ** [a-q] Letter between a and q ** [A-Q] Upper case letter between A and Q ** [0-7] Digit between 0 and 7 ** \n nth group/subpattern **
Caracteres Especiales
\ Escape Character **
\n New line **
\r Carriage return **
\t Tab **
\v Vertical tab **
\f Form feed **
\a Alarm
[\b] Backspace
\e Escape
\N{name} Named Character
Sustitución de cadenas $n nth non-passive group $2 "xyz" in /^(abc(xyz))$/ $1 "xyz" in /^(?:abc)(xyz)$/ $` Before matched string $' After matched string $+ Last matched string $& Entire matched string $_ Entire input string $$ Literal "$"
Modificadores de Patrones g Global match i Case-insensitive m Multiple lines s Treat string as single line x Allow comments and white space in pattern e Evaluate replacement U Ungreedy pattern
Metacaracteres que deben ser escapados
^ $ ( ) < [ { \ | > . * + ?
NOTA: los que están marcados con ** deberían funcionar en la mayoría de implementaciones de Expresiones Regulares.
Ejemplos de Patrones
([A-Za-z0-9-]+) Letras, números y guiones
(\d{1,2}\/\d{1,2}\/\d{4}) Fecha (21/3/2006)
([^\s]+(?=\.(jpg|gif|png))\.\2) imagenes jpg, gif o png
(^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$) Cualquier número del 1 al 50 inclusive
(#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?) Código de color hexadecimal válido
((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}) Cadena de 8 a 15 caracteres con al menos un letra mayúscula, una minúscula y un dígito (útil para contraseñas)
(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}) Direción de Email
(\<(/?[^\>]+)\>) Etiquetas HTML
NOTA: estos son meros ejemplos que deben usarse con cuidado y están puestos como referencia.
Articulos similares:
- Añadir soporte de formatos al QuickLook (29 diciembre 2008)
- Teclas rápidas en el CLI de Cisco (31 marzo 2009)
- Comprobar integridad de los ficheros Passwd y Group (20 enero 2009)
- Cisco Networkers 2009 (25 enero 2009)
- VPN site2site en Cisco ASA (2 octubre 2008)
Publicado: 11 de marzo de 2009 | Categorias: Sistemas
Revisado: 15 de febrero de 2010 | etiquetas: AWK, Bash, Documentacion, Linux, Perl, SED, Shell, Sistemas, Trucos, Vim
Revisado: 15 de febrero de 2010 | etiquetas: AWK, Bash, Documentacion, Linux, Perl, SED, Shell, Sistemas, Trucos, Vim

































Tremendo y utilísimo, muchas gracias por compartir esta información.