Demystifying Regular Expressions

"Some people, when confronted with a problem, think 'I know, I'll use
regular expressions.' Now they have two problems."
-- Jamie_Zawinski, on comp.lang.emacs

Regular expressions can be a powerful tool if you take the time to learn them. The way I use them they serve as a powerful find and replace tool.  I first learned about them from my experiences with Perl in the 1990s. Later I learned that they are highly related to theoretical computer science concepts such as a deterministic finite automaton, which are an awful lot like a finite state machine, which leads on to the Turing machine and all the way into super abstract concepts like the halting problem.

But this article is about demystifying, and for that simplicity is a prerequisite. To that end, we will dispense with the theoretical mumbo jumbo and get down to brass tacks. This article seeks to get you started using regular expressions in your job as soon as possible!

Regular Expression Playgrounds

Thanks to Julia Evans for linking to some mind-blowing regular expression playgrounds! You can use these to familiarize yourself with regular expressions and get started using them right away.

For Python: Pythex: a Python regular expression editor
For many languages: regex101: build, test, and debug regex
For Ruby: Rubular: a Ruby regular expression editor
For JavaScript / Perl Compatible Regular Expressions: RegExr: Learn, Build, & Test RegEx

Pro Tip: These playgrounds are not guaranteed to be standards compliant. See Regular Expression standard gap.

Search and Replace

My first deep dive into regular expressions happened near the year 2000. At the time we used a tool known as Search And Replace for Windows that basically blew my mind.

What I Use Now

Python https://docs.python.org/3/library/re.html

grep grep(1) - Linux manual page (man7.org)

Vim Find and Replace in Vim / Vi | Linuxize

Reg Ex Humor:

Image Credit: https://xkcd.com/208/

Previous
Previous

My Favorite Linux Aliases

Next
Next

Useful Utils