Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created March 22, 2024 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HariSekhon/9bd5a1d8e1eb1e1e57a84aa818baee2c to your computer and use it in GitHub Desktop.
Save HariSekhon/9bd5a1d8e1eb1e1e57a84aa818baee2c to your computer and use it in GitHub Desktop.
regex.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowlege-Base

Regex

Regular expressions are a core skill for any half decent programmer.

I use them extensively in languages from Python and Perl to Java/Scala/Groovy and even in shell scripts in Bash for grep, sed & awk.

Online Regex Testing

https://regex101.com/

PCRE vs BRE vs ERE

PCRE - Perl Compatible Regular Expressions

The gold standard from Perl which most popular languages aspire to

GNU grep has a grep -P switch to use PCRE but beware it's not portable. It won't work on BSD based systems like macOS.

On Mac you can install coreutils to get the better GNU Grep

brew install coreutils

but then you'll have to use the ggrep command instead.

Your shell scripts will have to figure our if they're on Mac and override the grep command (examples in DevOps-Bash-tools repo).

BRE - Basic Regular Expression

This is the neutered regex that old grep uses.

ERE - Extended Regular Expressions

Slightly better than BRE but still weak & awkward compared to PCRE.

Don't support back references.

Grep on most systems can support EREs via the grep -E switch.

Awk also uses EREs.

Core Reading

Master Regular Expressions

Library of Regex in Perl

PCRE regex:

HariSekhon/lib

Library of Regex in Python

PCRE regex:

HariSekhon/pylib

Library of Regex in Bash

BRE / ERE Regex:

HariSekhon/DevOps-Bash-tools

Examples of Real-world Regex Used Extensively

PCRE regex - see especially anonymize.py / anonymize.pl in these repos among many other scripts:

HariSekhon/DevOps-Python-tools

HariSekhon/DevOps-Perl-tools

HariSekhon/Nagios-Plugins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment