Expression Engine Usage

Hi,

i have an expression that can evaluate to “rootfolder\\subfolder”. I want to replace the double backslash with a single one, but have problems creating the correct expression.

These expressions validate, but fail to produce the desired value:
Folder\\SubFolder.Replace("\\","\")
“Folder\\SubFolder”.Replace("\\","\")

This one does not validate:
$Folder\\SubFolder.Replace("\\","\")$

Any help appreciated.

Thomas

I can use this expression:
$Utils.GetString(“Branch: Folder//SubFolder”).Replace("//","/")$

However this can not be validated:
$Utils.GetString(“Branch: Folder\\SubFolder”).Replace("\\","\")$

The error message is somewhat strange:
or " but got: \. - “bFolder”).Replace("\\","^\")$"

If the backslash is the error here, how do i have to escape it? Doubling it does not work.

Hi Thomas,

The issue here is that the backslash escapes the " character in function parameters. This is so we can allow the following expression $Utils.GetString("He said \"Yes\"").Replace("\"Yes\"",""\"No\"")$

The error you are seeing with $Utils.GetString(“Branch: Folder\\SubFolder”).Replace("\\","\")$ is because the backslash is escaping the final quote of each parameter. The expression parser is expecting another ".

Unfortunately, you can’t currently escape the escape character. We have been working on this today, and the next version with allow you to escape the backslashes as follows $Utils.GetString(“Branch: Folder\\\\SubFolder”).Replace("\\\\","\\")$. This should be available in the next couple of days.

I’m guessing that you won’t be entering Folder\\SubFolder directly (otherwise you could just enter Folder\SubFolder but would be using a variable, so note that the syntax to use would be $Utils.GetString(%branchFolder%).Replace("\\\\","\\")$

Hi Sparky,

this is good news :slight_smile:

Doubling the backslash is what i tried in first place.

Indeed i am modifying a string that gets created dynamically.

Such as:
\Folder1\Folder2\$Source.MyRepo.BranchName.Replace(“master”,"")$\$Build.Version$

In case the word “master” gets deleted, the resulting string will contain a double backslash.

That is what i want to use eventually:
$Utils.GetString("\Folder1\Folder2\$Source.MyRepo.BranchName.Replace(“master”,"")$\$Build.Version$ “).Replace(”\\","\")$

In other words, the created string will be embraced by a call to Utils.GetString.

You won’t be able to put expressions inside strings. Your expression will transpose to this:

$Utils.GetString("\\Folder1\\Folder2\\").Append($Source.MyRepo.BranchName.Replace("master","")$).Append("\\").Append($Build.Version$).Replace("\\","\")$

You may however find it simpler to use If actions and Set Variable actions. e.g.

If [$Source.MyRepo.BranchName$] Equals [master]
    Set-Variable [%myFolder%] [\Folder1\Folder2\$Build.Version$]
Else
    Set-Variable [%myFolder%] [\Folder1\Folder2\$Source.MyRepo.BranchName$\$Build.Version$]

Thanks Dave,

i’ll give the first option a try as soon as the next build will be released. I don’t consider CI’s actions for my use case, as the expression is more complex as what i have provided for demonstration purposes.

Hi Dave,

thanks a lot for version 1.9.1.318 which is fixing the reported issue.

This is my final expression:

$Utils.GetString("\\Folder1\\Folder2").Append("\\").Append($Source.MyRepo.BranchName$).Replace("master","").Replace("release\/.*","",True).Append("\\").Append($Build.Version$).Replace("\\\\","\\")$

Thomas

1 Like