PCRE2 is a Perl-compatible regular expression library that provides pattern-matching capabilities for applications. It provides command-line utilities such as pcre2grep and pcre2test, which can be used to search text with regular expressions and test regex patterns. This tutorial explains how to install PCRE2 utilities on Ubuntu 26.04.
Install PCRE2 Utilities
First, refresh the local package index to ensure the latest package information is available:
sudo apt update
Install the PCRE2 command-line utilities:
sudo apt install -y pcre2-utils
Once the package has been installed, check the versions of the included utilities to confirm if ready for use:
pcre2grep --version
pcre2test --version
Testing PCRE2 Utilities
A simple text file can be used to verify regular expression matching. Create test.txt containing a pattern followed by several test strings:
printf "/Hello (\w+)/\nHello World\nHello John\nSomething here\n" > test.txt
Run the test file with pcre2test:
pcre2test test.txt
A successful test produces output like this:
/Hello (\w+)/
Hello World
0: Hello World
1: World
Hello John
0: Hello John
1: John
Something here
No match
The regular expression matches lines beginning with Hello followed by a word. The captured word is shown separately in the output.
Uninstall PCRE2 Utilities
When the PCRE2 utilities are no longer required, remove the package with unused dependencies using the command:
sudo apt purge --autoremove -y pcre2-utils
Leave a Comment
Cancel reply