Match.group()#
relationalai.std.re
#Match.group(index: int|str|Producer = 0) -> Expression
Returns a subgroup captured by a regular expression match.
For an integer index
, .group()
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
, then .group()
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. Default is 0 , which returns the entire match. |
Returns#
An Expression
object.
Example#
Use .group()
to extract subgroups from a match by index:
#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 .group() filters out non-strings and non-matching strings, the
# following does not set properties for the Person objects with ID 2 and 3.
person.set(
full_name=match.group(0), # Get the entire match
first_name=match.group(1), # Get the first subgroup
last_name=match.group(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 be accessed by name:
#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.group(), "full_name"),
alias(match.group("first"), "first_name"), # Get the subgroup named "first"
alias(match.group("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 subscript notation:
#match[1] # Equivalent to match.group(1)
match["first"] # Equivalent to match.group("first")