diff --git a/macOS/build.sh b/macOS/build.sh
index 3c19bb923821b5f1a5e0c2e8aa6e69ac583267a2..10fcf8964eadf72ed9f3ecc9e5c7d60b93ef9f5f 100755
--- a/macOS/build.sh
+++ b/macOS/build.sh
@@ -55,7 +55,7 @@ LIB64="$ROOTDIR"/macOS/deps/"$PKG_ARCH"/lib64
 ## - the macOS linker is different from GNU ld and does not have the equivalent of -Bstatic/-Bdynamic
 ## - libgfortran.spec does not include --as-needed on macOS, hence it will link the library anyways
 ## Also, it does not seem possible to override libgfortran.spec with the --specs option.
-GCC_VERSION=$(sed -n "s|c = '$(BREWDIR)/bin/gcc-\([0-9]*\)'|\1|p" "$ROOTDIR"/scripts/homebrew-native-$PKG_ARCH.ini)
+GCC_VERSION=$(sed -En "/^c[[:space:]]*=/s/c[[:space:]]*=[[:space:]]*'.*gcc-([0-9]+)'/\1/p" "$ROOTDIR"/scripts/homebrew-native-"$PKG_ARCH".ini)
 
 QUADMATH_DIR=$(mktemp -d)
 ln -s $BREWDIR/opt/gcc/lib/gcc/$GCC_VERSION/libquadmath.a $QUADMATH_DIR
diff --git a/macOS/deps/Makefile b/macOS/deps/Makefile
index 6786c009d9425d959a6e518f60fac12082991064..316b2776dc4b9798158560affec5b0107cfadd7e 100644
--- a/macOS/deps/Makefile
+++ b/macOS/deps/Makefile
@@ -22,7 +22,7 @@ DEPS_ARCH ?= x86_64 # use x86_64 by default
 
 BREWDIR := $(if $(filter arm64,$(DEPS_ARCH)),/opt/homebrew,/usr/local)
 
-GCC_VERSION = $(shell sed -n "s|c = '$(BREWDIR)/bin/gcc-\([0-9]*\)'|\1|p" ../../scripts/homebrew-native-$(DEPS_ARCH).ini)
+GCC_VERSION = $(shell sed -En "/^c[[:space:]]*=/s/c[[:space:]]*=[[:space:]]*'.*gcc-([0-9]+)'/\1/p" ../../scripts/homebrew-native-$(DEPS_ARCH).ini)
 
 ROOT_PATH = $(realpath .)