Saturday 16 November 2013

Regular expressions matching - regex or regexp



Regular expressions matching:

/hello/ -> work hello should be there in the string

/^hello/ -> ^ mean, the match string should be in start
/hello$/ -> $ mean, the match string should be at the end

/hel+o/ -> + mean, once or more [ "helo", "hello", "helllllllo"] -> with one "l", or 2 "l" or multple "l"
/hel*o/ -> * mean, zero or more time. Almost same as above, with true for "heo" too. ]  -> [ "heo", "helo", "hello", "hellllllo" ]
/hel?o/ -> ? mean, zero or with only one. [ "heo" or "helo" only. ]. This will not match [ hello, hellllllo ]

/hello|goodbye/ -> either|or
/he..o/ -> . mean any character . [ hello, heazo ]
/\wello/ -> \w mean alphanmeric or _ (underscore)
/\bhello/ -> \b mean word boundary => space tab.


example of matching one of a email address:
/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/

Few more example of regex:

1. regex to grep the mac address:

egrep -io "([0-9A-F][0-9A-F]:)"{5}"[0-9A-F][0-9A-F]"

ifconfig | grep -i -P '([0-9A-F]{2}:){5}([0-9A-Fa-f][0-9A-F])'

-P for support of perl regex.

2. regex for ipaddress
ifconfig | egrep -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"

3.
 hostname  | egrep "(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])"

Notes on Regex:

*) for i in {1..9}; do touch $i.html; done

*) for i in `ls |grep html| cut -d "." -f1`; do mv ${i}.html ${i}.htm; done

/[crnld]ope/ -> [...] mean range of chars => mean will match ["cope" "rope", "nope", "lope" and "dope"] but not aope.


cat log1 |egrep -o -e "(client|appserver/webapp)/.*/" | perl -pe 's|(client\|appserver/webapp)|mydir|'

cat log1 |egrep -o -e "(client|appserver/webapp)/.*/" | perl -pe 's/(client|appserver\/webapp)/patch12/'

*** The above one liner will grep the above regex from the log1 file and do a search and replace, where ever "client" is there or "(appserver/webapp)" is there to "mydir" . ***


External links:
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
http://www.cs.tut.fi/~jkorpela/perl/regexp.html
http://ult-tex.net/info/perl/
http://cloud.github.com/downloads/tartley/python-regex-cheatsheet/cheatsheet.pdf
http://www.regular-expressions.info/
http://overapi.com/regex/

No comments:

Post a Comment