compile()#
relationalai.std.re
#compile(regex: str|Producer) -> Pattern
Compiles a regular expression string into a Pattern
object.
You can use the resulting Pattern
object to match strings with the regular expression, for example using its .match()
and .search()
methods.
Parameters#
Name | Type | Description |
---|---|---|
regex | str or Producer | A regular expression string or a Producer object that produces a regular expression string. |
Returns#
A Pattern
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="Bob")
Person.add(name="Jo")
Person.add(name="Jane")
# =======
# EXAMPLE
# =======
# Compile a regular expression pattern.
pattern = re.compile(r"J.*")
# Use the pattern to match names of people in the model.
with model.query() as select:
person = Person()
# Filter people whose names match the pattern
pattern.match(person.name)
response = select(person.name)
print(response.results)
# name
# 0 Jane
# 1 Jo