Match.__getitem__()#
#Match.__getitem__(index: int|str|Producer) -> Expression
Returns a subgroup captured by a regular expression match, using subscript notation.
For an integer index, .__getitem__() returns the subgroup at that index, where 0 is the entire match.
For a string index, it returns the subgroup with that name.
If index is a Producer, .__getitem__() filters out non-integer and non-string values from the producer.
Must be used in a rule or query context.
Parameters#
| Name | Type | Description |
|---|---|---|
index | Producer or Python int or str | The index or name of the subgroup to return. |
Returns#
An Expression object.
Example#
The .__getitem__() method allows access to subgroups in a match by index or name using subscript notation:
#import relationalai as rai
from relationalai.std import re
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
Person.add(id=1).set(name="Alan Turing")
Person.add(id=2).set(name="Bob")
Person.add(id=3).set(name=-1) # Non-string name
# =======
# EXAMPLE
# =======
with model.rule():
person = Person()
# Extract the first and last names of each person.
match = re.match(r"(\w+) (\w+)", person.name)
# Since .__getitem__() filters out non-strings and non-matching strings, the
# following does not set properties for Person objects with ID 2 and 3.
person.set(
full_name=match[0], # Get the entire match
first_name=match[1], # Get the first subgroup
last_name=match[2], # Get the second subgroup
)
with model.query() as select:
person = Person()
response = select(
person.id,
person.name,
person.full_name,
person.first_name,
person.last_name
)
print(response.results)
# id name full_name first_name last_name
# 0 1 Alan Turing Alan Turing Alan Turing
# 1 2 Bob NaN NaN NaN
# 2 3 -1 NaN NaN NaN
Named groups can also be accessed using subscript notation:
#from relationalai.std import alias
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"), # Get the subgroup named "first"
alias(match["last"], "last_name"), # Get the subgroup named "last"
)
print(response.results)
# full_name first_name last_name
# 0 Alan Turing Alan Turing
Alternatively, you can access subgroups using the .group() method:
#match.group(1) # Equivalent to match[1]
match.group("first") # Equivalent to match["first"]