Skip to main content
deleted 443 characters in body; edited title
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 317

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

  • 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.)
    (see update below: use single quote ')

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

  • 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.)
    (see update below: use single quote ')

How do quotes work in bash regex regarding reserved characters?

  • 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
    
added 492 characters in body
Source Link
Nor.Z
  • 133
  • 5
  • 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.)(Was expecting a way to write literal regex, that would be so much easier, seems not possible.)
    (see update below: use single quote ')

UpdateUpdate:

  • conclusion from comments & discussions:
    • use ' single quote & a variable outside the if statement.
    • here, ' in bash is actually analogous to / in js
      • -- so, the literal regex I mean (should have learnt more before asking.)
        (you may read comments & discussions below)
    • though, escaping single quote ' in bash -- '\'', is not as pretty as escaping / in js -- \/.)
  • 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 "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.)
    (see update below: use single quote ')

Update:

  • conclusion from comments & discussions:
    • use ' single quote & a variable outside the if statement.
    • here, ' in bash is actually analogous to / in js
      • -- so, the literal regex I mean (should have learnt more before asking.)
        (you may read comments & discussions below)
    • though, escaping single quote ' in bash -- '\'', is not as pretty as escaping / in js -- \/.)
added 346 characters in body
Source Link
Nor.Z
  • 133
  • 5

js anology:

const str = "The pizza is 2' and 100$price";

if (/[0-9]+' and [0-9]+\$price/.test(str)) {
  console.log('literal regex');
}
if (new RegExp("[0-9]+' and [0-9]+\\$price").test(str)) {
  console.log('non literal regex');
}

enter image description here

js anology:

const str = "The pizza is 2' and 100$price";

if (/[0-9]+' and [0-9]+\$price/.test(str)) {
  console.log('literal regex');
}
if (new RegExp("[0-9]+' and [0-9]+\\$price").test(str)) {
  console.log('non literal regex');
}

enter image description here

added 526 characters in body
Source Link
Nor.Z
  • 133
  • 5
Loading
Became Hot Network Question
focus que more on quotes
Source Link
Nor.Z
  • 133
  • 5
Loading
added 303 characters in body
Source Link
Nor.Z
  • 133
  • 5
Loading
Source Link
Nor.Z
  • 133
  • 5
Loading