Base regular Expressions (basic Regular expression) 
 
  
   
   | Serial Number |  
   symbols |  
   Description |  
   Example |  
  
 
  
  
   
   | 1 |  
   ^ |  
   To match with ... Line starting with |  
   ^ab matches lines starting with AB |  
  
 
   
   | 2 |  
   $ |  
   To match with ... End of Line |  
   ab$ matches lines ending with AB |  
  
 
   
   | 3 |  
   ^$ |  
   Match Blank Line |  
   ^$ matches blank lines, does not match spaces |  
  
 
   
   | 4 |  
   . |  
   Match any single character |  
   AB. Match ABC or ABD, mismatch ABCD or Abde, including spaces |  
  
 
   
   | 5 |  
   \ |  
   Escape character, escaping special symbols |  
   A\.B match a.b, mismatch AJB |  
  
 
   
   | 6 |  
   * |  
   Match previous item 0 or more times |  
   ab* match A or AB or abbb |  
  
 
   
   | 7 |  
   .* |  
   Match any character |  
   ab.* Match AB or ABC or ABCD, including blank lines |  
  
 
   
   | 8 |  
   [] |  
   Matches any single character within the collection |  
   AB[CD] matches ABC or ABD, does not match ABJ or ABCD |  
  
 
   
   | 9 |  
   [^] |  
   Match any single character outside the collection |  
   AB[^CD] matches Abe or ABJ, does not match ABC or ABD |  
  
 
  
 
[[email protected] ~]# vim linbin.txtI am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y
 
 
  
  - To match with ... Line starting with
  
 
 
[[email protected] ~]# grep "^m" linbin.txtmy blog is http://oldboy.blog.51cto.commy qq num is 49000488.my god,i am not oldbey,but OLDBOY
 
 
  
  - To match with ... End of Line
  
 
 
[[email protected] ~]# grep "m$" linbin.txtmy blog is http://oldboy.blog.51cto.com
 
[[email protected] ~]# grep -vn "^$" linbin.txt1:I am oldboy teacher!2:I teach linux.4:I like badminton ball,billard ball and chinese chess.6:my blog is http://oldboy.blog.51cto.com7:our site is http://www.etiantian.org9:my qq num is 49000488.11:not 4900000448.12:my god,i am not oldbey,but OLDBOY13:oldb y
 
 
  
  - Match any single character
  
 
 
[[email protected] ~]# grep "." linbin.txt  ##不匹配空行I am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y[[email protected] ~]# grep "oldb.y" linbin.txtI am oldboy teacher!my blog is http://oldboy.blog.51cto.commy god,i am not oldbey,but OLDBOYoldb y
 
[[email protected] ~]# grep "\.$" linbin.txtI teach linux.I like badminton ball,billard ball and chinese chess.my qq num is 49000488.not 4900000448.
 
 
  
  - Match previous item 0 or more times
  
 
 
[[email protected] ~]# grep "0*" linbin.txtI am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y[[email protected] ~]# grep -o "0*" linbin.txt  ##精确匹配字符串00000000
 
[[email protected] ~]# grep ".*" linbin.txt  ##匹配空行I am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y
 
 
  
  - Matches any single character within the collection
  
 
 
[[email protected] ~]# grep "[abc]" linbin.txtI am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy god,i am not oldbey,but OLDBOYoldb y[[email protected] ~]# grep "[0-9]" lb.txtmy blog is http://oldboy.blog.51cto.commy qq num is 49000488.not 4900000448.ss
 
 
  
  - Match any single character outside the collection
  
 
 
[[email protected] ~]# grep "[^a-z]" linbin.txt  ##匹配非小写字母I am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y[[email protected] ~]# grep "[^A-Z]" linbin.txt  ##匹配非大写字母I am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y[[email protected] ~]# grep "[^0-9]" linbin.txt  ##匹配非数字I am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYoldb y
Extended Regular expression (Extended Regular expression) 
 
  
   
   | Serial Number |  
   symbols |  
   Description |  
   Example |  
  
 
  
  
   
   | 1 |  
   + |  
   Match previous item 1 or more times |  
   ab+ matches AB or ABB, does not match a |  
  
 
   
   | 2 |  
   ? |  
   Match previous item 0 or 1 times |  
   AB matches A or AB, does not match ABB |  
  
 
   
   | 3 |  
   The |  
   Any one of the two sides |  
   Ab |  
   CD Match AB or CD |  
  
 
   
   | 4 |  
   () |  
   Match expression |  
   A (c |  
   d) e matches ace or ADE, does not match AE |  
  
 
   
   | 5 |  
   {N,m} |  
   Match previous item n~m times |  
   ab{2,3} matches ABB or ABBB and needs to {} be escaped |  
  
 
   
   | 6 |  
   {N,} |  
   Matches the preceding item at least n times, containing n times |  
   Ab{2,} matches ABB or ABBB and needs to {} be escaped |  
  
 
   
   | 7 |  
   N |  
   Match previous item n times |  
   AB{2} matches ABB, need to {} be escaped |  
  
 
   
   | 8 |  
   {, M} |  
   Matches the previous item up to M times, including M times |  
   ab{,2} matches a or AB or ABB, need to {} be escaped |  
  
 
  
 
[[email protected] ~]# vim linbin.txtI am oldboy teacher!I teach linux.I like badminton ball,billard ball and chinese chess.my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000488.not 4900000448.my god,i am not oldbey,but OLDBOYgoodgooodgd
 
 
  
  - Match previous item 1 or more times
  
 
 
[[email protected] ~]# grep -E "go+d" linbin.txtmy god,i am not oldbey,but OLDBOYgoodgoood
 
 
  
  - Match previous item 0 or 1 times
  
 
 
[[email protected] ~]# grep -E "go?d" linbin.txtmy god,i am not oldbey,but OLDBOYgd
 
 
  
  - Match | Any one of the two sides
  
 
 
[[email protected] ~]# grep -E "god|good" linbin.txtmy god,i am not oldbey,but OLDBOYgood
 
[[email protected] ~]# grep -E "g(la|oo)d" linbin.txtgood[[email protected] ~]# egrep "g(la|oo)d" linbin.txtgood[[email protected] ~]# egrep "g(oo)?d" linbin.txtgoodgd
 
 
  
  - Match previous item n~m times
  
 
 
[[email protected] ~]# grep -E "0{3,5}" linbin.txtmy qq num is 49000488.not 4900000448.
 
 
  
  - Match the preceding item at least n times
  
 
 
[[email protected] ~]# grep -E "0{3,}" linbin.txtmy qq num is 49000488.not 4900000448.
 
 
  
  - Match previous item n times
  
 
 
[[email protected] ~]# grep -E "0{3}" linbin.txtmy qq num is 49000488.not 4900000448.
Linux Regular expressions