sub()#

relationalai.std.re
#sub(
    regex: str|Producer,
    repl: string|Producer,
    string: str|Producer
) -> Expression

Filters strings that match a regular expression and replaces the matches with a string. Must be used in a rule or query context.

Parameters#

NameTypeDescription
regexstr or ProducerA regular expression string.
replstr or ProducerThe string to replace the matches with.
stringstr or ProducerThe string to match against.

Returns#

An Expression object.

Example#

#import relationalai as rai
from relationalai.std import alias, re

# =====
# SETUP
# =====

model = rai.Model("people")
Person = model.Type("Person")

with model.rule():
    Person.add(name="Alan Turing")
    Person.add(name="Gottfried Wilhelm Leibniz")


# =======
# EXAMPLE
# =======

with model.query() as select:
    person = Person()
    new_name = re.sub("Wilhelm", "W.", person.name)
    response = select(
        person.name,
        alias(new_name, "new_name")
    )

print(response.results)
#                         name              new_name
# 0  Gottfried Wilhelm Leibniz  Gottfried W. Leibniz

See Also#