replace()#

relationalai.std.strings
replace(string: Producer, old: str | Producer, new: str | Producer) -> Expression

Replaces all occurrences of a substring in a string. Must be called in a rule or query context.

Parameters#

NameTypeDescription
stringstr or ProducerA string or a producer that produces string values.
oldstr or ProducerThe substring to replace.
newstr or ProducerThe substring to replace old with.

Returns#

An Expression object.

Example#

import relationalai as rai
from relationalai.std.strings import replace

# Create a model named "people" with a Person type.
model = rai.Model("people")
Person = model.Type("Person")

# Add some people to the model.
with model.rule():
    alice = Person.add(name="Alice")
    bob = Person.add(name="Bob")

# Replace all occurrences of "ice" with "icia".
with model.query() as select:
    person = Person()
    replaced_name = replace(person.name, "ice", "icia")
    response = select(replaced_name)

print(response.results)
# Output:
#     name       v
# 0  Alice  Alicia
# 1    Bob     Bob