pcretest Command in Linux



The pcre2test command in Linux is a Perl program that tests the Perl-compatible regular expression (PCRE). It allows checking how a regex pattern behaves against various inputs and options. It was originally designed to test the PCRE library but can also be used for regex experimentation.

Note − pcretest command has been deprecated and replaced with its enhanced version, pcre2test.

Table of Contents

Here is a comprehensive guide to the options available with the pcretest command −

Note − The pcretest command is a deprecated tool for PCRE1, while pcre2test is the modern alternative for PCRE2 with enhanced features.

Installation of pcre2test Command in Linux

The pcre2test command is a part of the pcre2-utils package. To use it, install the pcre2-utils package on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions using the following command −

sudo apt install pcre2-utils

To install it on Arch Linux, use the command given below −

sudo pacman -S pcre2

To install pcre2test on Fedora, use the following command −

sudo dnf install pcre2-tools

To verify the installation of the pcre2test command, check its version using the -version option −

pcre2test -version
pcretest Command in Linux1

Syntax of pcre2test Command

The syntax of the pcre2test command in Linux is as follows −

pcre2test [options] [input file [output file]]

In the above syntax, the [options] field is used to specify optional flags to modify behavior.

The [input_file] field is used to specify the file containing regex patterns and test data (optional). The [output_file] is a file where the results are saved (optional).

pcre2test Commmand Options

The options of the pcre2test command are listed below −

Option Description
-8 Use the 8-bit library if built (default if available). Errors if not built..
-16 Uses the 16-bit library if built. Default if the 8-bit library is unavailable. Errors if not built.
-32 Uses the 32-bit library if built. Default if no other library is available. Errors if not built.
-ac Adds automatic callouts to each pattern.
-AC Same as -ac, but also shows extra callout information.
-b Outputs the full internal binary form of the pattern after compilation.
-C C Displays PCRE2 version and build configuration, then exits.
-C option Outputs specific build-time information and exits.
-d Outputs compiled pattern details, equivalent to -b -i.
-dfa Uses the pcre2_dfa_match() function instead of pcre2_match().
-error number Displays error messages for specified error numbers and exits.
-help Displays a summary of options and exits.
-i Outputs compiled pattern information.
-jit Enables JIT compilation if available.
-jitfast Uses JIT compilation with fast-path optimization.
-jitverify Uses JIT compilation and verifies JIT execution.
-LM Lists available pattern and subject modifiers and exits.
-LP Lists recognized Unicode properties and exits.
-LS Lists recognized Unicode script names and exits.
-pattern modifier-list Applies specified pattern modifiers.
-q Suppresses version number output at startup.
-S size Sets runtime stack size in mebibytes (MB).
-subject modifier-list Applies specified subject modifiers.
-t Measures compile and match times, iterating by default 500,000 times. Can specify count (e.g., -t 1000).
-tm Times only the matching phase.
-T, -TM Like -t and -tm, but also outputs total times.
-version Displays the PCRE2 version number and exits.

Examples of pcre2test Command in Linux

The section explores how to use the pcre2test command in Linux with examples −

  • Using pcre2test in Interactive Mode
  • Using pcre2test in Non-interactive Mode
  • Displaying pcre2test Configuration
  • Checking the JIT Compilation
  • Checking the DFA Compilation
  • Displaying an Error Message
  • Displaying the Compiled Pattern Information
  • Displaying the Usage Help

Using pcre2test in Interactive Mode

The pcre2test command is an interactive tool. To launch the interactive session, execute the command −

pcre2test
pcretest Command in Linux2

First, type the pattern (regular expression). For example, to search for sample word from the provided data, use −

/sample/
pcretest Command in Linux3

Next, type the data from which the match is needed to be obtained −

Hello World! This is a sample data.

The matched data will be displayed as shown in the following output image −

pcretest Command in Linux4

To make the match case-insensitive, use the /i flag −

/hello/i

The above command matches Hello from the data string despite the case difference.

pcretest Command in Linux5

To exit the interactive session, press CTRL+D or CTRL+C.

Using pcre2test in Non-interactive Mode

The pcre2test command can also be used in a non-interactive way. For example, create a file that contains data and patterns using the following command −

echo -e "/world/\nWelcome to Linux world" > file.txt

Now, process the file using the pcre2test command −

pcre2test file.txt
pcretest Command in Linux6

Displaying pcre2test Configuration

To display the pcre2test configuration, use the -C option −

pcre2test -C
pcretest Command in Linux7

Checking the JIT Compilation

JIT compilation is a technique that compiles code at runtime rather than before execution. It bridges the gap between interpreted execution (slower) and ahead-of-time (AOT) compilation (faster but less flexible).

In PCRE2, JIT compilation optimizes regular expression matching by converting patterns into highly efficient machine code. This speeds up pattern matching, especially for frequently used expressions.

To enable the JIT session, use the -jit option −

pcre2test -jit
pcretest Command in Linux8

Now, all the compilations that happen in this session will be JIT. To verify the JIT compilation, use the -jitverify option −

pcre2test -jitverify
pcretest Command in Linux9

Checking the DFA Compilation

DFA (Deterministic Finite Automaton Matching) matching is an alternative regex matching approach that processes the entire input in a single pass without backtracking. Unlike traditional backtracking-based matching, DFA-based matching guarantees linear time complexity in most cases.

To enable the DFA compilation, use the -dfa option −

pcre2test -dfa

Displaying an Error Message

To display the error message of a specific error number, use the pcre2test command in the following way −

pcre2test -error -21
pcretest Command in Linux10

Displaying the Compiled Pattern Information

To display the compiled pattern information, use the -i option −

pcre2test -i
pcretest Command in Linux11

Displaying the Usage Help

To display the usage help, use the -help option −

pcre2test -help

Conclusion

The pcre2test command in Linux is a powerful tool for testing and experimenting with Perl-compatible regular expressions (PCRE2). It provides various options for modifying regex behavior, testing pattern matching, and optimizing performance using JIT and DFA compilation. Installation is straightforward across different Linux distributions, and it can be used interactively or with input files.

Despite replacing the deprecated pcretest command, pcre2test remains essential for regex testing and troubleshooting.

Advertisements