Bash Regular Expression Condition

I have a regex that needs to be checked. The regex has double quotes in it, but I can't figure out how to avoid them correctly.

The first attempt does not work because quotation marks are not escaped.

while read line do if [[ $line =~ "<a href="(.+)">HTTP</a>" ]]; then SOURCE=${BASH_REMATCH[1]} break fi done < tmp/source.html echo "{$SOURCE}" #output = {"link.html"} (with double quotes) 

How can I run this correctly so that the output is link.html without double quotes.

I tried...

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

No luck. Can someone please help me so that I can stop banging my head on my table? I'm not big with bash. Thanks!

+7
source share
4 answers

It is always better to put your regular expression 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) 
+9
source
 $line =~ "<a href=\"(.+)\">HTTP</a>" 
+2
source

I recommend always using a variable when specifying a regular expression:

 #!/bin/bash SOURCE= url_re='<a href="(.+)">HTTP</a>' while read line do if [[ "$line" =~ $url_re ]]; then SOURCE=${BASH_REMATCH[1]} break fi done < test.txt echo $SOURCE # http://example.com/ # test.txt contents: # <a href="http://example.com/">HTTP</a> 
+1
source

Try "<a href="""(.+)""">HTTP</a>"

Change try this

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

or

'<a href="(.+)">HTTP</a>'

or

'<a href='\"'(.+)'\"'>HTTP</a>' <- this will give the correct syntax in Bash, as for a regular expression (. +), it does not know how it will be played

Change what you get when you use this regex "<a href=(.+)>HTTP</a>" ??

0
source

All Articles