Bash: exact match of a string with regex

I have to do an exact match on a string with a regex. i have to realize this pattern: toCheck must start with 2 occurrence of $str1, with max 1 occurrence of $str2. If toCheck match the pattern, i have to write $toCheck:success inside output, $toCheck:failed otherwise.

I wrote this regex:

regex="$1($1)+$2"

Using the site regexr.com, i inserted for example:

regex="lo(lo)ba"
toCheck="loloba"

It's a success.

On regerx.com, this match until the last ba:

toCheck="lolobaba"

but, on my bash code, it's a success.

This is the complete code:

toCheck="lolobaba"
regex="lo(lo)+ba"

if [[ $toCheck =~ $regex ]]; then

    echo "$toCheck:success" > output
else
    echo "$toCheck:failed" > output
fi

So, the question is: how to have an exact match between the string and the regex?


You get a success because the regex matches on a portion of it.

If you want an exact match, you need to anchor the pattern to the start and end of the line: regex="^lo(lo)+ba$"

  • the ^ stands for the start of the string: nothing can be before the pattern
  • the $ stands for the end of the string: nothing can be after

In your original code, as the pattern is not anchored, the pattern matching does not care of what could be before of after, if at least a portion of the string validates the pattern.


What are the parenthesis for? must start with 2x $str1 and end with max 1x $str2

So it's

    if [[ $toCheck =~ ^$str1$str1($str2|)$ ]] ; then
        echo "$toCheck:success" > output
    else
        echo "$toCheck:failed" > output
    fi