Match#
relationalai.std.re
#class Match
Represents a match returned by a regular expression operation, such as match()
or search()
.
Must be used in a rule or query context.
Attributes#
Name | Type | Description |
---|---|---|
.pos | int or Producer | The value of pos passed to match() or search() . This represents the starting position of the match or search. |
.re | Pattern | The compiled regular expression object whose .match() or .search() method returned this Match object. |
.string | str or Producer | The string passed to match() or search() . |
Methods#
Name | Description | Returns |
---|---|---|
.group() | Returns a subgroup of the match. | Producer |
.__getitem__() | Identical to .group() . Allows subscript access to individual groups from a match. | Expression |
.start() | Returns the starting position of the match. | Expression |
.end() | Returns the ending position of the match. | Expression |
.span() | Returns a tuple containing the starting and ending positions of the match. | tuple[Expression] |
Example#
Use the match()
or search()
functions to get a Match
object:
#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="Barbara Liskov")
# =======
# EXAMPLE
# =======
with model.query() as select:
person = Person()
# Match the pattern against the name of each person.
# Note that match() returns a Match object.
match = re.match(r"(\w+) (\w+)", person.name)
# Get the first and last names of each person.
first_name = match.group(1)
last_name = match.group(2)
response = select(
person.name,
alias(first_name, "first_name"),
alias(last_name, "last_name"),
)
print(response.results)
# name first_name last_name
# 0 Alan Turing Alan Turing
# 1 Barbara Liskov Barbara Liskov