warning warning warning This is a work-in-progress draft version.



PCRE2

Use -P option to enable Perl Compatible Regular Expressions 2 (PCRE2) instead of default Rust regex. PCRE2 is mostly similar, but not exactly same as regular expressions present in Perl programming language. The main feature difference between PCRE and PCRE2 is substitution.

This chapter will only show a few examples. For complete documentation and other information, see pcre: current doc.

Difference between GNU grep and ripgrep

Most of the features covered by grep -P will work exactly the same with rg -P as well. There could be differences with regards to how certain things are handled between GNU grep and ripgrep — for example, -f and -e options, empty matches, etc.

$ # empty match handling
$ echo '1a42z' | grep -oP '[a-z]*'
a
z
$ echo '1a42z' | rg -oP '[a-z]*'

a

z

$ cat five_words.txt 
sequoia
subtle
questionable
exhibit
equation
$ printf 'sub\nbit' | grep -P -f- five_words.txt
grep: the -P option only supports a single pattern
$ printf 'sub\nbit' | rg -P -f- five_words.txt
2:subtle
4:exhibit

Lookarounds

Examples for lookarounds and \K.

$ rg -P '(?=.*a)(?=.*e)(?=.*i)(?=.*o).*u' five_words.txt
1:sequoia
3:questionable
5:equation

$ echo 'hey food! foo42 foot5 foofoo' | rg -P 'foo(?!\d)' -r 'X'
hey Xd! foo42 Xt5 XX

$ # match if 'go' is not there between 'at' and 'par'
$ echo 'fox,cat,dog,parrot' | rg -qP 'at((?!go).)*par' && echo 'Match'
Match

$ # same as: rg -o '^(?:.*?cat.*?){2}(cat[a-z]*)' -r '$1'
$ s='scatter cat cater scat concatenate abdicate'
$ echo "$s" | rg -oP '^(.*?cat.*?){2}\Kcat[a-z]*'
cater

Backreference within regex definition

$ # remove any number of consecutive duplicate words separated by space
$ echo 'aa a a a 42 f_1 f_1 f_13.14' | rg -P '\b(\w+)( \1)+\b' -r '$1'
aa a 42 f_1 f_13.14

Mixing regex and literal matching

$ expr='(a^b)'
$ echo 'f*(2-a/b) - 3*(a^b)-42' | rg -oP '\S*\Q'"$expr"'\E\S*'
3*(a^b)-42

Automatically switch to PCRE2

If you wish to use Rust regex normally and switch to PCRE2 when needed, use the --engine=auto option.

$ # using a feature not present normally
$ echo 'car bat cod map' | rg -o '\b(bat|map)\b(*SKIP)(*F)|\w+'
regex parse error:
    \b(bat|map)\b(*SKIP)(*F)|\w+
                  ^
error: repetition operator missing expression

$ # automatically switch to PCRE2
$ echo 'car bat cod map' | rg -o --engine=auto '\b(bat|map)\b(*SKIP)(*F)|\w+'
car
cod

info See my blog post Search and replace tricks with ripgrep for more examples.