findall()#
relationalai.std.re
#findall(regex: str|Producer, string: str|Producer) -> tuple[Expression]
Filters strings that match a regular expression and returns a tuple (index, string)
representing all non-overlapping matches, where index
is the 1-based index of the match and string
is a matched string.
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 tuple of Expression
objects.
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()
# Filter people whose full name contains two words.
ix, matched_string = re.findall(r"(\w+)", person.name)
response = select(
person.name,
alias(ix, "index"),
alias(matched_string, "matched_string")
)
print(response.results)
# name index matched_string
# 0 Alan Turing 1 Alan
# 1 Alan Turing 2 Turing
# 2 Gottfried Wilhelm Leibniz 1 Gottfried
# 3 Gottfried Wilhelm Leibniz 2 Wilhelm
# 4 Gottfried Wilhelm Leibniz 3 Leibniz
Note that the index returned by fullmatch()
is the 1-based index of the match and not the position of the match in the string.