findall()#

relationalai.std.re.Pattern
#Pattern.findall(string: str|Producer, pos: int|Producer = 0) -> 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. Similar to the findall() function, but also accepts an optional pos parameter to specify the starting position of the search. 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 search. (Default: 0).

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

pattern = re.compile(r"(\w+)")

with model.query() as select:
    person = Person()
    # Filter people whose full name contains two words.
    ix, matched_string = pattern.findall(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.

See Also#