Bash Regular Expression Condition

It's always best to put your regex in a variable.

pattern='<a href="(.+)">HTTP</a>'
while read line
do
  if [[ $line =~ $pattern ]]; then
    SOURCE=${BASH_REMATCH[1]}
    break
  fi
done < tmp/source.html

echo "{$SOURCE}" #output = {link.html} (without double quotes)

If you quote the right hand side (the pattern), it changes the match from regex to a simple string equal (=~ effectively becomes ==).

As a side note, escaping is done with backslashes (\) rather than slashes (/), but that would not help your situation because of the outer quotes as mentioned in my previous paragraph.


$line =~ "<a href=\"(.+)\">HTTP</a>"