How to replace in variable strings inside azure-pipelines.yaml?

As @4c74356b41 pointed out. You can try adding powershell scripts to replace the Build.SourceBranch in your yaml build definition. And output the new sourcebranch to a new variable. Then you can use the new variable in the next steps.

Below is just a simple example. Click here for more information

1, replace "/" to "_" for SourceBranch and set the replaced value to variable newSourceBranch

- powershell: |
    $newbranch = "$(Build.SourceBranch)" -replace "/", "_"  
    echo "##vso[task.setvariable variable=newSourceBranch;isOutput=true]$newbranch"
  name: branchsetter 

2, Use the newSourceBranch in the following steps.

- powershell: |
    write-host "$(branchsetter.newSourceBranch)"

For others finding this through searches, there is now a replace expression: MS Doc

Roughly like this to replace underscores with hypens:

variables:      
  suffix: $[replace(variables['build.sourcebranchname'], '_', '-')]

I don't think you can do something like that natively yet, what I've been doing is the following:

  - bash: |
      date=$(date --rfc-3339=ns | sed "s/ /T/; s/\(\....\).*-/\1-/g")
      echo "##vso[task.setvariable variable=CONTAINER_BUILD_TIME]$date"

basically using a script step to set a specific value for a specific variable, and later on you can use it like you normally would: $(date)