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 Ojambo.com 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
Numbers
[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”
Email
(‘/^[a-zA-Z0-9.-_]+@
[a-zA-Z0-9.-_]+\.
([a-zA-Z](2,4))/’
Email@domain.com
Grab tags
(<img).+?> Grab img tag
(<img).+?(‘|”)> Grab img tag
Strip tags
/(<iframe|<object).+?
(</iframe|</object>)/gi
Strip out object and iframe tags

Text Editor Regular Expression

Image Missing
Geany – Regular Expressions

Conclusion:

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.

    Recommendations:

  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.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP and Mastering Blender Python API, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📖 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.