About the matter that the re.compiled object can be used for the re.match pattern

The story of poking the corner of the re module

re_spam.py


# -*- coding: utf-8 -*-
import re

re.match("spa*m", "spam") #Recognize

spam = re.compile("spa*m")
spam.match("spaaaaaaaam") #Recognize

re.match(spam, "spaaaam") #It works for some reason

The regular expression object obtained by re.compile as described above can be used for the re.match pattern. It seems that it is tentatively determined whether the passed as a pattern is a regular expression object.

Is there any merit in this method? The processing speed is slower than using re.match.

Transition

This is the source of Python-1.5.2 (excerpt)

re.py


def _cachecompile(pattern, flags=0):
    key = (pattern, flags)
    try:
        return _cache[key]
    except KeyError:
        pass
    value = compile(pattern, flags)
    if len(_cache) >= _MAXCACHE:
        _cache.clear()
    _cache[key] = value
    return value

def match(pattern, string, flags=0):
    return _cachecompile(pattern, flags).match(string)

def compile(pattern, flags=0):
    "Compile a regular expression pattern, returning a RegexObject."
    groupindex={}
    code=pcre_compile(pattern, flags, groupindex)
    return RegexObject(pattern, flags, code, groupindex)

There is no particular check. Then Python-2.0.1 sre.py (excerpt)

sre.py


def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)

def _compile(*key):
    # internal: compile pattern
    p = _cache.get(key)
    if p is not None:
        return p
    pattern, flags = key
    if type(pattern) not in sre_compile.STRING_TYPES:
        return pattern
    try:
        p = sre_compile.compile(pattern, flags)
    except error, v:
        raise error, v # invalid expression
    if len(_cache) >= _MAXCACHE:
        _cache.clear()
    _cache[key] = p
    return p

It seems that pattern is returned as it is if it is not a compileable string. And Python-2.7.9 re.py (excerpt)

re.py


def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)

def _compile(*key):
    # internal: compile pattern
    pattern, flags = key
    bypass_cache = flags & DEBUG
    if not bypass_cache:
        cachekey = (type(key[0]),) + key
        try:
            p, loc = _cache[cachekey]
            if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
                return p
        except KeyError:
            pass
    if isinstance(pattern, _pattern_type):
        if flags:
            raise ValueError('Cannot process flags argument with a compiled pattern')
        return pattern
    if not sre_compile.isstring(pattern):
        raise TypeError, "first argument must be string or compiled pattern"
    try:
        p = sre_compile.compile(pattern, flags)
    except error, v:
        raise error, v # invalid expression
    if not bypass_cache:
        if len(_cache) >= _MAXCACHE:
            _cache.clear()
        if p.flags & LOCALE:
            if not _locale:
                return p
            loc = _locale.setlocale(_locale.LC_CTYPE)
        else:
            loc = None
        _cache[cachekey] = p, loc
    return p

In addition to checking whether the pattern is a string, it also checks whether it has been compiled, and clearly states "first argument must be string or compiled pattern".

Recommended Posts

About the matter that the re.compiled object can be used for the re.match pattern
Functions that can be used in for statements
About the matter that torch summary can be really used when building a model with Pytorch
[Django] About users that can be used on template
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
How to set variables that can be used throughout the Django app-useful for templates, etc.-
A timer (ticker) that can be used in the field (can be used anywhere)
Python standard module that can be used on the command line
Understand the probabilities and statistics that can be used for progress management with a python program
I created a template for a Python project that can be used universally
Hide the warning that zsh can be used by default on Mac
Mathematical optimization that can be used for free work with Python + PuLP
File types that can be used with Go
About the matter that localhost: 4040 cannot be accessed after running Spark with Docker
About the matter that was worried about sampling error
The problem that the ifconfig command cannot be used
Overview and useful features of scikit-learn that can also be used for deep learning
File sharing server made with Raspberry Pi that can be used for remote work
Basic algorithms that can be used in competition pros
Python knowledge notes that can be used with AtCoder
ANTs image registration that can be used in 5 minutes
4 boxes that might be useful for the Pepper hackathon
List the classes that can be referenced by ObjCClass
About the Visitor pattern
[Django] Field names, user registration, and login methods that can be used in the User model
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
Miscellaneous notes that I tried using python for the matter
Scripts that can be used when using bottle in Python
I investigated the pretreatment that can be done with PyCaret
[Flask] I tried to summarize the "docker-compose configuration" that can be created quickly for web applications
I tried to expand the database so that it can be used with PES analysis software
Which octal literals can be used depends on the programming language
About character string handling that can be placed in JSON communication
Python standard input summary that can be used in competition pro
Can the Kalman filter be used to predict stock price trends?
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
Solution to the problem that Ctrl + z cannot be used in Powershell in Docker for windows environment (provisional)
A memo when creating an environment that can be debugged with Lambda @ Edge for the time being
Must-see for new engineers! The technical characteristics of your company that can be understood through web training!