ReplaceStrRegex

ReplaceStrRegex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )
replace_str_regex( value, pattern, replace, [ first_only ] )
ReplaceStrRegex( value, pattern, replace, [ first_only ] )

Description

The ReplaceStrRegex function returns a string which has either the first or all occurrences of the pattern replaced with the replace string. The pattern conforms to Java regular expression syntax.

Parameters

Argument Type Definition and Requirements

value

The source string to perform replacements within.

pattern

The Java regular expression to be found in the source string.

replace

The new string that replaces the sub-string found by pattern.

first_only

Optional - When true, replace only the first occurrence of the pattern. Default is false.

Since Go does not have function overloading or optional arguments, you must use the function f.OnlyFirst() instead of a literal true.

Returns

A string which has either the first or all occurrences of the pattern replaced with the replace string.

Examples

The following query executes an array of independent ReplaceStrRegex operations and returns the results in an array. The result array position matches the execution array position.

The individual operations are:

  1. The string "One Fish Two Fish" is processed, replacing the pattern "Two" with the string "Blue". The result string "One Fish Blue Fish" is placed in the top position of the result array.

  2. The string "One Fisk Two FisT" is processed, replacing every string matching the pattern "Fis." with the string "Fish". The result string "One Fish Two Fish" is placed in the second position of the result array.

  3. The same string "One Fisk Two FisT" is processed, but because it has the first_only flag set to true, only the first string matching the pattern "Fis." is replaced. The result string "One Fish Two FisT" is placed in the third position of the result array.

Value result = await client.Query(
    Arr(
        ReplaceStrRegex("One Fish Two Fish", "Two", "Blue"),
        ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish"),
        ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish", true)
    )
);

Console.WriteLine(result);
Arr(StringV(One Fish Blue Fish), StringV(One Fish Two Fish), StringV(One Fish Two FisT))
result, err := client.Query(
	f.Arr{
		f.ReplaceStrRegex("One Fish Two Fish", "Two", "Blue"),
		f.ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish"),
		f.ReplaceStrRegex(
			"One Fisk Two FisT",
			"Fis.",
			"Fish",
			f.OnlyFirst(),
		),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[One Fish Blue Fish One Fish Two Fish One Fish Two FisT]
System.out.println(
    client.query(
        Arr(
            ReplaceStrRegex("One Fish Two Fish", "Two", "Blue"),
            ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish"),
            ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish", true)
        )
    ).get());
["One Fish Blue Fish", "One Fish Two Fish", "One Fish Two FisT"]
client.query(
  [
    q.ReplaceStrRegex('One Fish Two Fish', 'Two', 'Blue'),
    q.ReplaceStrRegex('One Fisk Two FisT', 'Fis.', 'Fish'),
    q.ReplaceStrRegex('One Fisk Two FisT', 'Fis.', 'Fish', true),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
[ 'One Fish Blue Fish', 'One Fish Two Fish', 'One Fish Two FisT' ]
result = client.query(
  [
    q.replace_str_regex("One Fish Two Fish", "Two", "Blue"),
    q.replace_str_regex("One Fisk Two FisT", "Fis.", "Fish"),
    q.replace_str_regex("One Fisk Two FisT", "Fis.", "Fish", True),
  ]
)
print(result)
["One Fish Blue Fish", "One Fish Two Fish", "One Fish Two FisT"]
println(Await.result(
    client.query(
        Arr(
            ReplaceStrRegex("One Fish Two Fish", "Two", "Blue"),
            ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish"),
            ReplaceStrRegex("One Fisk Two FisT", "Fis.", "Fish", true)
        )
    ),
    5.seconds
))
["One Fish Blue Fish", "One Fish Two Fish", "One Fish Two FisT"]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!