Quantcast
Channel: Forcing Bash to use Perl RegEx Engine - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 3

Forcing Bash to use Perl RegEx Engine

$
0
0

As you may already know, a lot of the features modern RegEx engines support (back referencing, lookaround assertions, etc.) are not supported by Bash RegEx engine. Following is a simple Bash script I have just created to try to explain what my end goal is:

#!/bin/bash

# Make sure exactly two arguments are passed.
if [ $# -lt 2 ]
then
    echo "Usage: match [string] [pattern]"
    return
fi

variable=${1}
pattern=${2}

if [[ ${variable} =~ ${pattern} ]]
then
    echo "true"
else
    echo "false"
fi

So for instance, something like the following command will return false:

. match.sh "catfish" "(?=catfish)fish"

whereas the exact same expression will find a match when used in a Perl or a JavaScript regex tester.

Backreferences (e.g. (expr1)(expr2)[ ]\1\2) won't match as well.

I have simply come to the conclusion that my problem will only be solved when forcing bash to use a Perl-compatible RegEx engine. Is this doable? If so, how would I go about performing the procedure?


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>