Warning
This page is located in archive.

Notes About Regular Expressions

Manual pages

(man 7 regex)
man -k regex

Basic usage

1. Matching
2. Substitute

grep --color -E pattern
sed 's/pattern/replacement/options'

Pattern examples

Special characters

^ $ . + ? { [ () / | \

Anchors

^, $

Alternatives

/^hold|^fold/
/^(hold|fold)/
/^(h|f)old/

Char classes

/[fhcb]old$/
/[a-zA-Z0-9]old$/
/[a-fA-F0-9x] hex$/
/[^a-z]/ is a negation 
/[a-z^]/ 
/[\^a-z]/ 

\d [0-9] (digit)
\D [^0-9]
\s [ \t\n\r\f] (white chars)
\S [^ \t\n\r\f]
\w [0-9a-zA-Z] (word chars)
\W [^0-9a-zA-Z]
. any character (!)

sed 's/All right\./OK/'

/ice\scream/

Quantifiers

/color|colour/
/colo(r|ur)/
/colou?r/
/colou{0,1}r/ 
/Xa{3}/  =  /Xaaa/
/(Xa){3}/ = /XaXaXa/
/xy*z/
/xy+z/

/^\d+$/
/ab?c[,.:]d/ abc.d ; ac,d ; ac:d
/xy|yz+/ 	xy ; yz ; yzzzzzz
/[\d\s]{2,3}/ 12, 123, '  ', '   ', '1 2', '\t\r2'

? {0,1}
+ {1,}
* {0,}

Priority

() 		-- grouping
? + * { } 	-- quantifiers
x \x $ ^ 	-- characters, anchors
| 		-- alternatives

Example

www.google.com
www.google-informatics.com
www.seznam.cz
www.linux.info

^www\.[a-z-]{1,}\.[a-z]{2,4}$
^w{3}\.[[:lower:]-]+\.[:lower:]{2,4}$
^www\.[a-z-]{1,}\.[a-z]{2,4}$

wget http://bio.felk.cvut.cz -O - | \
  sed 's/\n\r//gmi' | \
  sed 's/</\n</g' | \
  grep -o '<img .* />' | \
  grep -o 'src=\"[^\"]*\"'

echo 'a.b.c' | cut -d. -f2
echo 'a.b.c' | sed 's/\.//g
  sed 's/(..)(...)(..)/\2/'
  /^(\S+)\s.*\s\1$/

/"(.*)"/
"How are you," asked.
"How are you," asked, "could you come?"

/"([^"]+)"/

/"(.*?)"/
Lazy quantifiers: +?, *?, ??, {}?

BASH

if [[ "$var" =~ a[0-9] ]] ; then # do not quote the regexp pattern since bash 3.2

if [[ "$a" =~ ^[[:digit:]]+$ ]] ; then
	echo "${a} is a number"
fi

courses/ae3b33osd/regex.txt · Last modified: 2015/03/11 17:54 by bursam