fullmatch()#

relationalai.std.re
#fullmatch(regex: str|Producer, string: str|Producer) -> Match

Filters strings if the entire string matches a regular expression and returns a Match object. Must be used in a rule or query context.

Parameters#

NameTypeDescription
regexstr or ProducerA regular expression string.
stringstr or ProducerThe string to match against.

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="Gottfried Wilhelm Leibniz")


# =======
# EXAMPLE
# =======

with model.query() as select:
    person = Person()
    # Filter people whose full name contains two words.
    re.fullmatch(r"(\w+) (\w+)", person.name)
    response = select(person.name)

print(response.results)
#           name
# 0  Alan Turing

Compare that to the re.match() function:

#with model.query() as select:
    person = Person()
    # Filter people whose name starts with two words.
    re.match(r"(\w+) (\w+)", person.name)
    response = select(person.name)

print(response.results)
#                         name
# 0                Alan Turing
# 1  Gottfried Wilhelm Leibniz


# fullmatch() is equivalent to match() with '^' and '$' anchors.
with model.query() as select:
    person = Person()
    re.match(r"^(\w+) (\w+)$", person.name)
    response = select(person.name)

print(response.results)
#           name
# 0  Alan Turing

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

See Also#