__getitem__()#
relationalai.std.re.Match
#Match.__getitem__(index: int|str|Producer) -> Expression
Returns a subgroup captured by a regular expression match.
Equivalent to .group(index)
.
If index
is an integer, it returns the subgroup at that index, where 0
is the entire match.
If index
is a string, it returns the subgroup with that name.
Must be used in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
index | int , str , or Producer | The index or name of the subgroup to return. |
Returns#
An Expression
object.
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="Barbara Liskov")
# =======
# EXAMPLE
# =======
with model.query() as select:
person = Person()
# Extract the first and last names of each person.
match = re.match(r"(\w+) (\w+)", person.name)
response = select(
alias(match[0], "full_name"),
alias(match[1], "first_name"),
alias(match[2], "last_name"),
)
print(response.results)
# full_name first_name last_name
# 0 Alan Turing Alan Turing
# 1 Barbara Liskov Barbara Liskov
Named groups can be accessed by name:
#with model.query() as select:
person = Person()
# Extract the first and last names of each person using named groups.
match = re.match(r"(?P<first>\w+) (?P<last>\w+)", person.name)
response = select(
alias(match[0], "full_name"),
alias(match["first"], "first_name"),
alias(match["last"], "last_name"),
)
print(response.results)
# full_name first_name last_name
# 0 Alan Turing Alan Turing
# 1 Barbara Liskov Barbara Liskov