Skip to main content
5 of 8
added 526 characters in body
Nor.Z
  • 133
  • 5

How quotes work in bash regex expression? (Regarding special regex reserved character & special bash reserved characters.)

Is there any special bash reserved characters in bash regex expression?

  • ex:

    • eg: if [[ $url =~ ^https:\/\/www\.youtube\.com\/playlist\?(.+&)?list= ]]; then echo "URL matches the regex"; fi
      This is just a normal regex test.
  • problem:

    • Do I need to be careful about special case where:
      I have things like eg: %VAR%, $VAR, &VAR, (or any potential special bash reserved characters), inside my regex expression.
      Do I need to escape those?
    • eg: if [[ $url =~ ^https:\/\/%VAR%/$VAR/&VAR/!@#$%^&*()-=_+/wte;/ ]]; then echo "URL matches the regex"; fi
      // assume I intentionally need to match such regex
  • ex:

  • again:

    • I am not asking about escaping the regex (I know how to use regex, be it PCRE or ERE doesnt matter.)
    • I am asking about escaping the bash reserved special characters, when inside a regex expression, if there is any.
  • In another words, is the regex expression in bash

    • more like /^http:\/\/www/g in js literal regex, where the inside will always be literal regex;
    • or more like "^http:\\/\\/www" in js RegExp, where you need to escape the reserved characters ' " ` \ etc due to being inside a string

Update:

  • For "Is there any special bash reserved characters in bash regex expression?"
    The simple answer is yes.

  • As for an example:

    url="https://www.youtube.com/playlist?list=PLmHVyfmcRKyxvxehq3fiGjKDsEyy6d4Tz"
    regex='^https:\/\/www\.youtube\.com\/playlist\?(.+&)?list='
    
    idx=0
    (( idx++ )) # normal
    if [[ $url =~ ^https:\/\/www\.youtube\.com\/playlist\?(.+&)?list= ]]; then echo "URL matches the regex - $idx"; fi
    (( idx++ )) # normal
    if [[ $url =~ $regex ]]; then echo "URL matches the regex - $idx"; fi
    (( idx++ ))
    yt='www\.youtube\.com\/'
    if [[ $url =~ ^https:\/\/${yt}playlist\?(.+&)?list= ]]; then echo "URL matches the regex - $idx"; fi
    (( idx++ ))
    if [[ $url =~ ^https:\/\/'${yt}'playlist\?(.+&)?list= ]]; then echo "URL matches the regex - $idx"; fi
    (( idx++ ))
    if [[ $url =~ '^https:\/\/'${yt}'playlist\?'(.+&)?list= ]]; then echo "URL matches the regex - $idx"; fi
    
  • I guess its better to change the question:
    from
    "Is there any special bash reserved characters in bash regex expression?"
    to
    "How quotes work in bash regex expression? (Regarding special regex reserved character & special bash reserved characters.)"

  • (Was expecting a way to write literal regex, that would be so much easier, seems not possible.)


Update:

For what literal regex that I want means:

text='The pizza is 2'\'' and 100$price' # The pizza is 2' and 100$price

# literal regex I want: [0-9]+' and [0-9]+\$price
regex="[0-9]+' and [0-9]+\\\$price"
if [[ "$text" =~ $regex ]]; then echo "this is the normal way of doing things, do you see I need to escape the \ & $ ?"; fi
# if [[ "$text" =~ [0-9]+' and [0-9]+\$price ]]; then echo "this is what I prefer to have -- literal regex, like / /g in js. But this wont even compile"; fi
Nor.Z
  • 133
  • 5