0

I have written a piece of code to test whether a string matches a domain like this:

host=$1
if [[ $host =~ ^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$ ]] ; then
    echo "it is a domain!"
fi

With help from this website but for some reason, the above is not working.

Do you have any idea why?

Jahid
  • 21,542
  • 10
  • 90
  • 108
maxisme
  • 3,974
  • 9
  • 47
  • 97
  • i think bash regex won't support lookarounds. And you're not allowed to use regex inside double quotes. – Avinash Raj Jun 17 '15 at 18:35
  • I have tried without quotes and still not working! – maxisme Jun 17 '15 at 18:40
  • ya, bash won't support lookarounds.. you may use perl. – Avinash Raj Jun 17 '15 at 18:42
  • "regular expressions" are not a specific language, but more of a paradigm. If you have an object written in Java, you can't copy-paste it to C++. If you have a regex written in PCRE, you can't copy-paste it to ERE. – that other guy Jun 17 '15 at 18:52
  • Yes I understand that I am now using the regex from a question focusing on bash code, [here](http://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation) – maxisme Jun 17 '15 at 18:53

1 Answers1

2

Bash regex doesn't have lookaround, you can use Perl Regex with grep:

#!/bin/bash
if grep -oP '^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}$' <<< "$1" >/dev/null 2>&1;then
echo valid
else
echo invalid
fi
Jahid
  • 21,542
  • 10
  • 90
  • 108