Add regex replace and function call docs

This commit is contained in:
elkowar 2021-05-14 11:55:08 +02:00
parent 54f6713ffe
commit c0d9b53004
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
2 changed files with 12 additions and 0 deletions

View file

@ -34,4 +34,7 @@ The expression language supports:
- strings can contain other expressions again: `'foo {{some_variable}} bar'`
- json access (`object.field`, `array[12]`, `object["field"]`)
- for this, the object/array value needs to refer to a variable that contains a valid json string.
- some function calls:
- `round(number, decimal_digits)`: Round a number to the given amount of decimals
- `replace(string, regex, replacement)`: Replace matches of a given regex in a string

View file

@ -230,6 +230,15 @@ fn call_expr_function(name: &str, args: Vec<PrimVal>) -> Result<PrimVal> {
}
_ => Err(anyhow!("Incorrect number of arguments given to {}", name)),
},
"replace" => match args.as_slice() {
[string, pattern, replacement] => {
let string = string.as_string()?;
let pattern = regex::Regex::new(&pattern.as_string()?)?;
let replacement = replacement.as_string()?;
Ok(PrimVal::from(pattern.replace_all(&string, replacement.replace("$", "$$").replace("\\", "$")).into_owned()))
}
_ => Err(anyhow!("Incorrect number of arguments given to {}", name)),
},
_ => Err(anyhow!("Unknown function {}", name)),
}
}