span()#

relationalai.std.re.Match
#Match.span() -> Expression

Returns a tuple containing the 0-based starting and ending position of the match. Must be used in a rule or query context.

Returns#

An Expression object.

Example#

#import relationalai as rai
from relationalai.std import alias, re

# =====
# SETUP
# =====

model = rai.Model("companies")
Company = model.Type("Company")

with model.rule():
    Company.add(name="RelationalAI")
    Company.add(name="Snowflake")


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

with model.query() as select:
    company = Company()
    # Filter companies whose name contains 'AI'.
    match = re.search(r"AI", company.name)
    # Get the start and end positions of the match.
    start, end = match.span()
    response = select(
        company.name,
        alias(start, "match_start"),
        alias(match.end(), "match_end"),
    )

print(response.results)
#            name  match_start  match_end
# 0  RelationalAI           10         12

Note that you can’t select the span() method directly. Instead, you must unpack the tuple returned by span() into separate variables:

## INCORRECT
with model.query() as select:
    company = Company()
    match = re.search(r"AI", company.name)
    response = select(
        company.name,
        match.span(),  # Does not work
    )

# CORRECT
with model.query() as select:
    company = Company()
    match = re.search(r"AI", company.name)
    start, end = match.span()
    response = select(company.name, start, end)

See Also#