1

I have followed this answer to implement the bash regex

#!/bin/bash
MYVAR=ho02123ware38384you443d34o3434ingtod38384day
MYVAR=${MYVAR//[[:alpha:]]/X} 
echo ${MYVAR//[[:digit:]]/N}

However, when I try with a more complex regex, it doesn't replace anything:

#!/bin/bash
PREVIOUS_CONFIGURATION=`cat /etc/apache2/sites-available/user-domain.conf`
NEW_CONFIGURATION=${PREVIOUS_CONFIGURATION//ServerName .+?\n/testbro} 
echo "$NEW_CONFIGURATION"
exit 1

This is how my file user-domain.conf looks like:

        ServerName CUSTOM_USER_DOMAIN
        ServerAdmin webmaster@localhost
        DocumentRoot /home/sifoo2/server2.wassap.io

        <Directory /home/sifoo2/server2.wassap.io/>
oguz ismail
  • 1
  • 16
  • 47
  • 69
Constantin
  • 848
  • 8
  • 23

2 Answers2

3

The pattern used in a pattern substitution word expansion is not a regular expression, but a glob, or an extended glob if extglob option is enabled. As a glob, .+? matches .+ and any single character following that; .+a, .+1, etc.

Your aim here is apparently performing a non-greedy/lazy/reluctant match but that's not possible with normal globs. However a workaround with extended globs is available (shortened variable names for better readability):

shopt -s extglob
new=${prev//ServerName +([!$'\n'])/testbro}

See:

oguz ismail
  • 1
  • 16
  • 47
  • 69
-2

Try it like this:

${var//ServerName\ [[:alpha:]]*/testbro}
lbal
  • 291
  • 2
  • 8