Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
4  
Why don't you just use perl instead of bash for scripting? And why is this question tagged javascript? –  Marco Jul 26 '13 at 1:06
    
Because using Bash is a must in my situation. And I have accidentally tagged JavaScript. I removed it :) –  Fadi Hanna AL-Kass Jul 26 '13 at 1:11
2  
Why don't you use grep with -P or using sed? –  cuonglm Jul 26 '13 at 1:32
2  
But you never explain the situation/problem that made you come to the conclusion that you must have the shell do something it simply can't do. There make be a better way. –  arx Jul 26 '13 at 2:25

1 Answer 1

up vote 4 down vote accepted

Bash doesn't support a method for you to do this at this time. You're left with the following options:

  1. Use Perl
  2. Use grep [-P|--perl-regexp]
  3. Use Bash functionality to code it

I think I would go with #2 and try and use grep to get what I want functionally. For back referencing you can do the following with grep:

$ echo 'BEGIN `helloworld` END' | grep -oP '(?<=BEGIN `).*(?=` END)'
helloworld

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

(?=pattern)
    is a positive look-ahead assertion
(?!pattern)
    is a negative look-ahead assertion
(?<=pattern)
    is a positive look-behind assertion
(?<!pattern)
    is a negative look-behind assertion 

References

share|improve this answer
    
I honestly didn't know grep had a [-P|--perl-regexp] token. Thanks a lot :-) –  Fadi Hanna AL-Kass Jul 29 '13 at 1:12
    
@FadiHannaAL-Kass - you're welcome. Thanks for the question. –  slm Jul 29 '13 at 1:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.