Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Debian GNU/Linux Reference Guide
Prev Home Next

4.4.1 Regular expressions

Regular expressions are used in many text processing tools. They are analogous to the shell wildcards (see Shell wildcards, Section 4.3.8), but they are both more complicated and more powerful.

The regular expression describes the matching pattern and is made up of text characters and metacharacters. The metacharacter is just a character with a special meaning. There are 2 major styles, BRE and ERE, depending on the text tools as described in Unix-like text processing, Section 4.4.

For the EREs, the metacharacters include "\ . [ ] ^ $ * + ? ( ) { } |". The regular expression means:

  • c

    • This matches the non-metacharacter "c".

  • \c

    • This matches the literal character "c".

  • .

    • This matches any character including newline.

  • ^

    • This matches the beginning of a string.

  • $

    • This matches the end of a string.

  • \<

    • This matches the beginning of a word.

  • \>

    • This matches the end of a word.

  • [abc...]

    • This character list matches any of the characters "abc...".

  • [^abc...]

    • This negated character list matches any of the characters except "abc...".

  • r*

    • This matches zero or more regular expressions identified by "r".

  • r+

    • This matches one or more regular expressions identified by "r".

  • r?

    • This matches zero or one regular expressions identified by "r".

  • r1|r2

    • This matches one of the regular expressions identified by "r1" or "r2".

  • (r1|r2)

    • This matches one of the regular expressions identified by "r1" or "r2" and treats it as a bracketed regular expression.

In BREs the metacharacters "+ ? ( ) { } |" lose their special meaning; instead use the backslashed versions "\+ \? \( \) \{ \} \|". Thus the grouping construct (r1|r2) needs to be quoted as \(r1|r2\) in BREs. Since emacs, although being basically BRE, treats "+ ?" as the metacharacters. Thus there are no needs to quote them. See Replacement expressions, Section 4.4.2 for how the grouping construct is used.

For example, grep can be used to perform the text search using the regular expression:

     $ egrep 'GNU.*LICENSE|Yoyodyne' /usr/share/common-licenses/GPL
                         GNU GENERAL PUBLIC LICENSE
                         GNU GENERAL PUBLIC LICENSE
       Yoyodyne, Inc., hereby disclaims all copyright interest in the program

Debian GNU/Linux Reference Guide
Prev Home Next

 
 
  Published under the terms of the GNU General Public License Design by Interspire