RE2 - A regular expression library with linear time guarantees
—
Essential pattern matching functions that provide the primary interface for regular expressions. These functions support searching for patterns within text, matching patterns at specific positions, and extracting subpatterns.
Searches for the first occurrence of a pattern anywhere in the input text, returning a match object if found.
def search(pattern, text, options=None):
"""
Search for pattern in text and return the first match.
Args:
pattern (str): Regular expression pattern
text (str): Input text to search
options (Options, optional): Compilation options
Returns:
_Match or None: Match object if pattern found, None otherwise
"""Example usage:
import re2
# Find email address in text
text = "Contact us at support@example.com"
match = re2.search(r'(\w+)@(\w+\.\w+)', text)
if match:
print(match.group()) # "support@example.com"
print(match.groups()) # ("support", "example.com")
print(match.start()) # 14
print(match.end()) # 33Matches a pattern only at the beginning of the input text, similar to anchoring the pattern with ^.
def match(pattern, text, options=None):
"""
Match pattern at the beginning of text.
Args:
pattern (str): Regular expression pattern
text (str): Input text to match
options (Options, optional): Compilation options
Returns:
_Match or None: Match object if pattern matches at start, None otherwise
"""Example usage:
import re2
# Match at beginning only
text = "Hello world"
match = re2.match(r'Hello', text)
print(match is not None) # True
match = re2.match(r'world', text)
print(match is not None) # False (doesn't match at beginning)Matches a pattern against the entire input text, requiring the pattern to match the complete string.
def fullmatch(pattern, text, options=None):
"""
Match pattern against the entire text.
Args:
pattern (str): Regular expression pattern
text (str): Input text to match
options (Options, optional): Compilation options
Returns:
_Match or None: Match object if pattern matches entire text, None otherwise
"""Example usage:
import re2
# Must match entire string
text = "123-45-6789"
match = re2.fullmatch(r'\d{3}-\d{2}-\d{4}', text)
print(match is not None) # True
match = re2.fullmatch(r'\d{3}', text)
print(match is not None) # False (doesn't match entire string)Finds all non-overlapping matches of a pattern in the input text, returning captured groups or full matches.
def findall(pattern, text, options=None):
"""
Find all matches of pattern in text.
Args:
pattern (str): Regular expression pattern
text (str): Input text to search
options (Options, optional): Compilation options
Returns:
list: List of matched strings or tuples of groups
"""Example usage:
import re2
# Find all digits
text = "Phone: 555-1234, Fax: 555-5678"
numbers = re2.findall(r'\d+', text)
print(numbers) # ['555', '1234', '555', '5678']
# Find with groups
matches = re2.findall(r'(\w+): (\d{3})-(\d{4})', text)
print(matches) # [('Phone', '555', '1234'), ('Fax', '555', '5678')]Returns an iterator of match objects for all non-overlapping matches, providing detailed information about each match.
def finditer(pattern, text, options=None):
"""
Return iterator of match objects for all matches.
Args:
pattern (str): Regular expression pattern
text (str): Input text to search
options (Options, optional): Compilation options
Returns:
iterator: Iterator of _Match objects
"""Example usage:
import re2
text = "The numbers are 42 and 123"
for match in re2.finditer(r'\d+', text):
print(f"Found '{match.group()}' at position {match.start()}-{match.end()}")
# Found '42' at position 16-18
# Found '123' at position 23-26class _Match:
"""Represents a successful regular expression match."""
def group(*groups):
"""
Return one or more subgroups of the match.
Args:
*groups: Group numbers or names (0 = entire match)
Returns:
str or tuple: Matched group(s)
"""
def groups(default=None):
"""
Return tuple of all groups.
Args:
default: Value for unmatched groups
Returns:
tuple: All captured groups
"""
def groupdict(default=None):
"""
Return dictionary of named groups.
Args:
default: Value for unmatched groups
Returns:
dict: Named groups mapping
"""
def start(group=0):
"""
Return start position of group.
Args:
group (int): Group number (0 = entire match)
Returns:
int: Start position
"""
def end(group=0):
"""
Return end position of group.
Args:
group (int): Group number (0 = entire match)
Returns:
int: End position
"""
def span(group=0):
"""
Return (start, end) tuple for group.
Args:
group (int): Group number (0 = entire match)
Returns:
tuple: (start, end) positions
"""
def expand(template):
"""
Expand template string using match groups.
Args:
template (str): Template with group references
Returns:
str: Expanded string
"""
# Properties
re: '_Regexp' # Pattern object that produced this match
string: str # String that was searched
pos: int # Start position of search
endpos: int # End position of search
lastindex: int # Index of last matched group
lastgroup: str # Name of last matched groupInstall with Tessl CLI
npx tessl i tessl/pypi-google-re2