compile()#
#compile(regex: str|Producer) -> Pattern
Compiles a regular expression string into a Pattern
object.
When passing a string literal, use a raw string — which is prefaced with an r
, as in r"J.*"
— to avoid escaping special characters in regular expressions.
Parameters#
Name | Type | Description |
---|---|---|
regex | Producer or Python str | The regular expression string to compile. |
Returns#
A Pattern
object.
Example#
Use the compile()
function to compile a regular expression into a Pattern
object:
#import relationalai as rai
from relationalai.std import re
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
Company = model.Type("Company")
with model.rule():
Person.add(id=1).set(name="Bob")
Person.add(id=2).set(name="Sue")
Person.add(id=3).set(name="Braxton")
with model.rule():
Company.add(id=1).set(name="RelationalAI")
Company.add(id=2).set(name="Snowflake")
# =======
# EXAMPLE
# =======
# Compile a regular expression pattern. If you pass a string literal to compile(),
# you may call compile() outside of a rule or query and reuse the Pattern object
# across multiple contexts.
pattern = re.compile(r"S.*")
# Get people whose names match the pattern.
with model.query() as select:
person = Person()
pattern.match(person.name) # Filter names that match the pattern.
response = select(person.id, person.name)
print(response.results)
# id name
# 0 2 Sue
# Get companies whose names match the pattern.
with model.query() as select:
company = Company()
pattern.match(company.name)
response = select(company.id, company.name)
print(response.results)
# id name
# 0 2 Snowflake
Pattern
objects created from Python string literals can be reused across multiple rule and query contexts.
You may also pass a Producer
object to compile()
.
However, in that case, the Pattern
object can only be used in the same rule or query context where it was created:
#Regex = model.Type("Regex")
with model.rule():
Regex.add(pattern=r"J.*")
Regex.add(pattern=r"B.*")
with model.rule():
regex = Regex()
# Compile each regex pattern. Note that regex.pattern is an InstanceProperty,
# which is a subclass of the Producer class.
pattern = re.compile(regex.pattern)
# Use the pattern object to assign Person objects whose names match the
# pattern to a multi-valued matched_people property.
person = Person()
pattern.match(person.name)
regex.matched_people.add(person)
# Get the names of people matched by each pattern.
with model.query() as select:
regex = Regex()
person = regex.matched_people
response = select(regex.pattern, person.name)
print(response.results)
# pattern name
# 0 B.* Bob
# 1 B.* Braxton
# 2 S.* Sue
# pattern was created from an InstanceProperty, so it can't be used in a
# different rule or query context.
with model.rule():
person = Person()
pattern.match(person.name) # Raises an error.
Since you can’t re-use Pattern
objects created from Producer
objects in different rule or query contexts, you may alternatively use the re.match()
function directly instead of pre-compiling the pattern:
## Alternative to the rule in the previous example that uses re.match() directly.
with model.rule():
regex = Regex()
person = Person()
re.match(regex.pattern, person.name)
regex.matched_people.add(person)
Note that you can’t set properties of objects to a Pattern
object.
Doing so raises an error:
#with model.rule():
Regex.add(pattern=re.compile(r"J.*")) # Raises an error.