logo
Tags down

shadow

Replace all String regex matches with environment variables in bash


By : Jessica Doran
Date : July 31 2020, 01:00 AM
I wish this help you I am trying to figure out how to replace matches of regex in a file with the value of an environment variable called a substring of that matched string. Each matched String also has a default along with it, separated by a colon. What I mean is if I have a file called myFile.properties that contains: , You may do it in pure BASH regex:
code :
re='^([^=]+=)\$\{([_a-zA-Z][_a-zA-Z0-9]*)(:([^}]*))?\}'

while IFS= read -r line; do
   if [[ $line =~ $re ]]; then                       # match regex
      #declare -p BASH_REMATCH

      var="${BASH_REMATCH[2]}"                       # var name in group #2
      if [[ -n ${!var+set} ]]; then                  # if var is set in env
         line="${BASH_REMATCH[1]}${!var}"            # use var value
      elif [[ -n ${BASH_REMATCH[4]} ]]; then.        # if default value is set
         line="${BASH_REMATCH[1]}${BASH_REMATCH[4]}" # use default string
      fi
   fi
   echo "$line"                                      # print each line
done < file.properties
varWithDefault=value1
varSetNoDefault=value2
varEmptyValue=
varEmptyValueWithDefault=
varNotSetWithDefault=I have : a colon
# required but no match will prob be an error message
varNotSetNoDefault=${notSetEnvVar}


Share : facebook icon twitter icon

Bash Scripting: Replace (or delete) string in a file if line starts with (or matches) another string


By : Asha Kiran
Date : March 29 2020, 07:55 AM
this one helps. Something like the following in sed should do what you need. First we check if the line starts with Icon= and if it does then we run the s command (i.e. substitute).
code :
sed -i '/^Icon=/s/\.ico$/.png/' file
/^Icon=/ {             # Only run the following block when this matches
    s/\.ico$/.png/     # Substitute '.ico' at the end of the line with '.png'
}

C# how to use Regex.replace, to replace all the pattern matches with the target string


By : user3568496
Date : March 29 2020, 07:55 AM
wish help you to fix your issue There is no \g or concept of a global search, as the .NET Regex class is global by default. In other words, it should just work, assuming you've written your regular expression properly.
You might want to test it in Regex Hero and then once you've got it working click the .NET button at the top to get the code with properly escaped strings.

replace string if regex matched and replace with something else it matches some other regex


By : FujiZ
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , By passing re.sub() a function instead of replacement string you could do it like this:
code :
def func(matchobj):
    return 'aaa_' if matchobj.group(0) == 'sss_' else 'bbb_'

def dic_gen(exc):
    param_errors = {re.sub(r'(sss_)|(ttt_)', func, err.name):
                       err.address for err in exc.errors}
    param_errors["status"] = "ERROR"
    return param_errors
def dic_gen(exc):
    param_errors = {re.sub(r'(sss_)|(ttt_)', 
        lambda mo: 'aaa_' if mo.group(0) == 'sss_' else 'bbb_', err.name):
        err.address for err in exc.errors}
    param_errors["status"] = "ERROR"
    return param_errors

How to strip or replace all matches of a pattern from a string in Bash?


By : Elli
Date : March 29 2020, 07:55 AM
wish helps you I am trying to write the dirname function in Bash so that it doesn't use any external commands. , If extglob is on:
code :
shopt -s extglob
printf '%s\n' "${path//\/+(\/)/\/}"

Regex (or not) replace all matches with dynamic variables


By : user3092759
Date : March 29 2020, 07:55 AM
I hope this helps you . You can replace them by using a function as second argument in String.prototype.replace().
code :
const cache = { firstvar: "dog", secondvar: "river", thirdvar: "cat" },
      text = "The @@firstvar@@ went to the @@secondvar@@ with the @@thirdvar@@.",
      regex = /@@(.*?)@@/g;

console.log( text.replace(regex, (_match, group1) => cache[group1]) );
shadow
Privacy Policy - Terms - Contact Us © voile276.org