Regular Expressions

Advanced search and replace using regular expressions

Basic find and replace operations are a form of regular expressions. Regular expressions are also referred to as regex or regexp. Advanced operations of regular expressions allow more control of matching patterns.

Matching patterns can be set to matching the entire document, the first instance, or part of a word.

This tutorial uses Perl-derivative regular expressions. The regular expressions can be used on any text file any any application that supports regular expressions. Many programming languages support regular expressions that are similar.

    Tools are required:

  • Text editor with support for regular expressions.
  • Word Processor with support for regular expressions.
  • Optional Perl-like language such as Awk, Perl or PHP.

Optional Download and install suitable text editor

Any text editor with regular expression support will work. To select a reviewed lightweight programming editor read the Lightweight Programming Editors.

Optional Download and install word processor

Regular Expressions Cheat Sheet

Anchors Description
^ Beginning position
$ Ending position
\b Word boundary
$ Ending position
Character Classes Description
\s White space (for not white space use “\S”)
\d Digit (for not digit use “\D”)
\w Word (for not word use “\W”)
Quantifiers Description
* Zero (0) or more
+ One (1) or more of preceding
? Zero (0) or One (1)
(min,max) Min/Max characters eg (4,10)
Logic Description
. Any Character eg .(6,9) for password
[] Range eg [abc]
[^] Not eg [^abc]
| Or eg gr(a|e)y gray or grey
[-] Between eg [a-c]
() Relationship eg ([a-z](4,10))
Special Characters Description
\ Escape Any Character
\r Carriage Return
# Comments eg # This is a comment
Pattern Modifiers Description
g Global match eg /every/g
i Ignore case eg /every/i
m Multiline eg /every/m
x Extended eg /every/gxm

Regular Expression Examples

Regular Expression Description
[0-9]+ First occurrence of a number
Multiple line matching
/^.*NEED.*$/gxm Matching all lines with “NEED”
/^.*NEED.*$\r/gxm Matching multiple lines with “NEED”
Grab tags
(<img).+?> Grab img tag
(<img).+?(‘|”)> Grab img tag
Strip tags
Strip out object and iframe tags

Text Editor Regular Expression

Image Missing
Geany – Regular Expressions


The regular expressions cheat sheet can be used in future projects. Keep the expressions simple and use as needed. The Perl-like syntax is the most common and can be applied in text editors, word processors and programming languages.


  1. This tutorial uses an optional lightweight programming editor with regular expression support.
  2. The regular expression concepts can be applied to other programming languages.
  3. Make your own cheat-sheet.

Tags: , , , , , ,

This entry was posted on Wednesday, July 20th, 2011 at 12:00 am and is filed under Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “Regular Expressions”

  1. Ojambo Regular Expressions Video 0027 Says:

    August 5th, 2011 at 12:03 am

    […] Video for the regular expressions article. Part One of […]

  2. Ojambo Alternative to JavaScript Video 0028 Says:

    August 12th, 2011 at 12:10 am

    […] Video for the alternative to JavaScript article. Part One of […]

  3. Ojambo JavaScript Replace Text Says:

    October 5th, 2011 at 12:03 am

    […] identifiers will be used to speedup the process of finding and replacing text. Tips from the regular expressions article will be used to find specific […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>