match()#

relationalai.std.re.Pattern
#Pattern.match(string: str|Producer, pos: int|Prodcuer = 0) -> Match

Filters strings that match a regular expression and returns a Match object. Similar to the match() function, but also accepts an optional pos parameter to specify the starting position of the match. Must be used in a rule or query context.

Parameters#

NameTypeDescription
stringstr or ProducerThe string to match against.
posint or ProducerThe starting position of the match. (Default: 0)

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
# =======

pattern = re.compile(r"A.*")

with model.query() as select:
    person = Person()
    # Filter people whose names start with 'A'.
    pattern.match(person.name)
    response = select(person.name)

print(response.results)

Note that raw strings are recommended for regular expression patterns to avoid issues with backslashes.

See Also#