0

I have a makefile (which uses the SHELL := /bin/bash target) that I would like to parse a string var in to retrieve a substring, I'm trying to make sense of the escaping rules for the bash regex language but it's not clicking for me. If I store the regex in a variable, I can use it in a bash line as follows:

$ RE="(\w+\/)*(\w{3}-[0-9]+).*"
$ [[ "foo/foo-123" =~ $RE ]] && echo ${BASH_REMATCH[2]}
foo-123
$

however if I try to use the regex in the command itself it will not work:

$ [[ "foo/foo-123" =~ (\w+\/)*(\w{3}-[0-9]+).* ]] && echo ${BASH_REMATCH[2]}
$

and even if I enclose it in quotes, or use:

$ [[ "foo/foo-123" =~ ^(\w+\/)*(\w{3}-[0-9]+).*$ ]] && echo ${BASH_REMATCH[2]}
$

it no longer matches. If I try to use the first solution in my makefile it also does not correctly parse the substring. I understand there are further escaping problems due to using $ in the makefile but I'm confused as to why I experience them in bash, is there a better way to achieve what I want to do in the makefile or if not, what am I missing to get this approach working?

ptr
  • 3,292
  • 2
  • 24
  • 48
  • in the first example you set RE and use $ID_RE. Does that explain the behaviour? – ruud May 31 '23 at 11:59
  • First of all, `\w+{3}` is most likely a typo, did you want `\w{3}`? Then, `\w` might not be supported, use `[[:alnum:]_]` instead for better POSIX regex compatibility. Make sure you use the right variables in the script. And if you have an issue with the makefile, please show the part of the makefile you have issues with. – Wiktor Stribiżew May 31 '23 at 12:00
  • apologies, i've corrected the typos – ptr May 31 '23 at 12:11

1 Answers1

0

\w is not supported in bash. Also, +{3} doesn't make much sense, use just {3}. Moreover, the final .* is not needed - the regex doesn't have to match the whole string.

[[ foo/foo-123 =~ ([[:alpha:]]+/)*([[:alpha:]]{3}-[0-9]+) ]] && echo ${BASH_REMATCH[2]}

In a Makefile, you need to double the dollar signs:

SHELL = /bin/bash
test:
    [[ foo/foo-123 =~ ([[:alpha:]]+/)*([[:alpha:]]{3}-[0-9]+) ]] && echo $${BASH_REMATCH[2]}

choroba
  • 231,213
  • 25
  • 204
  • 289
  • thanks, as an additional query could you elaborate on why it DOES work if the regex with `\w` is in a variable and substituted in the command? – ptr May 31 '23 at 12:15
  • 1
    Probably a bug in bash regex quoting. See also https://stackoverflow.com/q/9792702/1030675 – choroba May 31 '23 at 13:59