Skip to content

Latest commit

 

History

History
105 lines (60 loc) · 3.38 KB

regex.md

File metadata and controls

105 lines (60 loc) · 3.38 KB

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/

https://regexr.com/

Sed Regex Testing

Test your sed regex here:

https://sed.js.org/

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

Readme Card

Readme Card

Readme Card