Simple script for testing regular expressions in Perl

Simple script for testing regular expressions in Perl

At work the other day I was creating regular expressions (regexps) in perl for use with swatch. Instead of testing the regexps by entering them into the swatch config file, restart swatch, and use the “logger” command to trigger swatch to take action, I thought I’d simplify it by creating a small script. Since swatch is written i perl, I created a tiny script in perl so that there wouldn’t be any regexps mismatch between how the script interprets regexps and how swatch interprets them.

The script takes as input a text string and a regular expression, and prints the matched regexps (if any). In the example below I pass a simple regexps to the script to extract the hostname from a sample rsyslog entry:

The script itself looks like this:

Leave a Reply

%d bloggers like this: