match()#
relationalai.std.re
#match(regex: str|Producer, string: str|Producer) -> Match
Filters strings that match a regular expression and returns a Match
object.
Must be used in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
regex | str or Producer | A regular expression string. |
string | str or Producer | The string to match against. |
Returns#
A Match
object.
Example#
#import relationalai as rai
from relationalai.std import re
# =====
# SETUP
# =====
model = rai.Model("people")
Person = model.Type("Person")
with model.rule():
Person.add(name="Alan Turing")
Person.add(name="Barbara Liskov")
# =======
# EXAMPLE
# =======
with model.query() as select:
person = Person()
# Filter people whose names start with 'A'.
re.match(r"A.*", person.name)
response = select(person.name)
print(response.results)
Note that raw strings are recommended for regular expressions to avoid issues with backslashes.