Regular expressions summary

Well I'm honest - I wrote these lines cos I'm always forgetting the syntax of regexp and there is no good summary that fits my needs!

expressions

expression meaning examples description
[ ] bracket [a-z] all characters from a to z
( ) (name) match the string name
[A-Z] all characters from A to Z
[a-z0-9] all characters from a to z or 0 to 9
. dot [0-9]. match any digit, match any character
{ } repetition [a-z]{2} there are exactly 2 characters from a to z
[a-z]{0,} there is 1 or more characters from a to z
[a-z]{2,5} there is 2 but not more than 5 characters from a to z
? optional-repetition [a-z]? there are 0 or 1 characters from a to z
* repetition [a-z]* there are 0 or more characters from a to z
+ existing-repetition [a-z]+ there are 1 or more characters from a to z
\b empty string at the edge of a word [0-9]\b any number, not included in a word
\B empty string not at the edge of a word [0-9]\B any number, in a word
^ beginning of a line ^(#) a line, that starts with #
$ end of a line (#)$ a line, that ends with #
| OR operator [a-z]| [0-9] means [a-z] OR [0-9]

general expressions used

checking for a valid e-mail address

\b([a-zA-Z0-9-_]+@{1}[a-zA-Z0-9-_]{2,}([a-zA-Z0-9-]+([.][a-zA-Z0-9]+[-]*[a-zA-Z0-9]+)+)+)

examples

  • user@host.de
  • user__@host.com.jp

checking for comments in a file

^#

examples

  • # This is a comment ....

fitting a hex-number

\b(0x)[0-9a-fA-F]{2,}[hH]?|\b(0x)?[0-9a-fA-F]{2,}[hH]

examples

  • 0x00h
  • 00h
  • 0x0000H

fitting a binary-number

\b[01]{2,}[bB]

examples

  • 01b
  • 01010B

getting a line from /etc/passwd for a special user

examples

?> PASSWD_ENTRY=$(/bin/grep --regexp="^$USER\b" /etc/passwd)
 
wiki/howto/general/regexp.txt · Last modified: 2005/10/17 18:21 by e-razor
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki