Skip to content

# `` regexmatching

This wrapper intercepts calls through the store interface which make use of the REGEXTerm class to represent matches by REGEX instead of literal comparison.

Implemented for stores that don’t support this and essentially provides the support by replacing the REGEXTerms by wildcards (None) and matching against the results from the store it’s wrapping.

Classes:

- `REGEXMatching` –
- `REGEXTerm` –

  REGEXTerm can be used in any term slot and is interpreted as a request to

Functions:

- `regexCompareQuad` –

Attributes:

- `NATIVE_REGEX` –
- `PYTHON_REGEX` –

## `` NATIVE_REGEX `module-attribute`

    NATIVE_REGEX = 0

## `` PYTHON_REGEX `module-attribute`

    PYTHON_REGEX = 1

## `` REGEXMatching

    REGEXMatching(storage)

Bases: `Store`

Methods:

- `__len__` –
- `add` –
- `bind` –
- `close` –
- `commit` –
- `contexts` –
- `destroy` –
- `namespace` –
- `namespaces` –
- `open` –
- `prefix` –
- `remove` –
- `remove_context` –
- `rollback` –
- `triples` –

Attributes:

- `context_aware` –
- `formula_aware` –
- `storage` –
- `transaction_aware` –

Source code in `rdflib/plugins/stores/regexmatching.py`

    46
    47
    48
    49
    50
    51
    52

    def __init__(self, storage):
        self.storage = storage
        self.context_aware = storage.context_aware
        # NOTE: this store can't be formula_aware as it doesn't have enough
        # info to reverse the removal of a quoted statement.
        self.formula_aware = storage.formula_aware
        self.transaction_aware = storage.transaction_aware

### `` context_aware `instance-attribute`

    context_aware = context_aware

### `` formula_aware `instance-attribute`

    formula_aware = formula_aware

### `` storage `instance-attribute`

    storage = storage

### `` transaction_aware `instance-attribute`

    transaction_aware = transaction_aware

### `` __len__

    __len__(context=None)

Source code in `rdflib/plugins/stores/regexmatching.py`

    143
    144
    145
    146

    def __len__(self, context=None):
        # NOTE: If the context is a REGEX this could be an expensive
        # proposition
        return self.storage.__len__(context)

### `` add

    add(triple, context, quoted=False)

Source code in `rdflib/plugins/stores/regexmatching.py`

    63
    64
    65

    def add(self, triple, context, quoted=False):
        (subject, predicate, object_) = triple
        self.storage.add((subject, predicate, object_), context, quoted)

### `` bind

    bind(prefix, namespace, override=True)

Source code in `rdflib/plugins/stores/regexmatching.py`

    158
    159

    def bind(self, prefix, namespace, override=True):
        self.storage.bind(prefix, namespace, override=override)

### `` close

    close(commit_pending_transaction=False)

Source code in `rdflib/plugins/stores/regexmatching.py`

    57
    58

    def close(self, commit_pending_transaction=False):
        self.storage.close()

### `` commit

    commit()

Source code in `rdflib/plugins/stores/regexmatching.py`

    170
    171

    def commit(self):
        self.storage.commit()

### `` contexts

    contexts(triple=None)

Source code in `rdflib/plugins/stores/regexmatching.py`

    148
    149
    150
    151
    152
    153

    def contexts(self, triple=None):
        # NOTE: There is no way to control REGEX matching for this method at
        # this level as it only returns the contexts, not the matching
        # triples.
        for ctx in self.storage.contexts(triple):
            yield ctx

### `` destroy

    destroy(configuration)

Source code in `rdflib/plugins/stores/regexmatching.py`

    60
    61

    def destroy(self, configuration):
        self.storage.destroy(configuration)

### `` namespace

    namespace(prefix)

Source code in `rdflib/plugins/stores/regexmatching.py`

    164
    165

    def namespace(self, prefix):
        return self.storage.namespace(prefix)

### `` namespaces

    namespaces()

Source code in `rdflib/plugins/stores/regexmatching.py`

    167
    168

    def namespaces(self):
        return self.storage.namespaces()

### `` open

    open(configuration, create=True)

Source code in `rdflib/plugins/stores/regexmatching.py`

    54
    55

    def open(self, configuration, create=True):
        return self.storage.open(configuration, create)

### `` prefix

    prefix(namespace)

Source code in `rdflib/plugins/stores/regexmatching.py`

    161
    162

    def prefix(self, namespace):
        return self.storage.prefix(namespace)

### `` remove

    remove(triple, context=None)

Source code in `rdflib/plugins/stores/regexmatching.py`

     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103

    def remove(self, triple, context=None):
        (subject, predicate, object_) = triple
        if (
            isinstance(subject, REGEXTerm)
            or isinstance(predicate, REGEXTerm)
            or isinstance(object_, REGEXTerm)
            or (context is not None and isinstance(context.identifier, REGEXTerm))
        ):
            # One or more of the terms is a REGEX expression, so we must
            # replace it / them with wildcard(s)and match after we query.
            s = not isinstance(subject, REGEXTerm) and subject or None
            p = not isinstance(predicate, REGEXTerm) and predicate or None
            o = not isinstance(object_, REGEXTerm) and object_ or None
            c = (
                (context is not None and not isinstance(context.identifier, REGEXTerm))
                and context
                or None
            )

            removeQuadList = []  # noqa: N806
            for (s1, p1, o1), cg in self.storage.triples((s, p, o), c):
                for ctx in cg:
                    ctx = ctx.identifier
                    if regexCompareQuad(
                        (s1, p1, o1, ctx),
                        (
                            subject,
                            predicate,
                            object_,
                            context is not None and context.identifier or context,
                        ),
                    ):
                        removeQuadList.append((s1, p1, o1, ctx))
            for s, p, o, c in removeQuadList:
                self.storage.remove((s, p, o), c and Graph(self, c) or c)
        else:
            self.storage.remove((subject, predicate, object_), context)

### `` remove_context

    remove_context(identifier)

Source code in `rdflib/plugins/stores/regexmatching.py`

    155
    156

    def remove_context(self, identifier):
        self.storage.remove((None, None, None), identifier)

### `` rollback

    rollback()

Source code in `rdflib/plugins/stores/regexmatching.py`

    173
    174

    def rollback(self):
        self.storage.rollback()

### `` triples

    triples(triple, context=None)

Source code in `rdflib/plugins/stores/regexmatching.py`

    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141

    def triples(self, triple, context=None):
        (subject, predicate, object_) = triple
        if (
            isinstance(subject, REGEXTerm)
            or isinstance(predicate, REGEXTerm)
            or isinstance(object_, REGEXTerm)
            or (context is not None and isinstance(context.identifier, REGEXTerm))
        ):
            # One or more of the terms is a REGEX expression, so we must
            # replace it / them with wildcard(s) and match after we query.
            s = not isinstance(subject, REGEXTerm) and subject or None
            p = not isinstance(predicate, REGEXTerm) and predicate or None
            o = not isinstance(object_, REGEXTerm) and object_ or None
            c = (
                (context is not None and not isinstance(context.identifier, REGEXTerm))
                and context
                or None
            )
            for (s1, p1, o1), cg in self.storage.triples((s, p, o), c):
                matchingCtxs = []  # noqa: N806
                for ctx in cg:
                    if c is None:
                        if context is None or context.identifier.compiledExpr.match(
                            ctx.identifier
                        ):
                            matchingCtxs.append(ctx)
                    else:
                        matchingCtxs.append(ctx)
                if matchingCtxs and regexCompareQuad(
                    (s1, p1, o1, None), (subject, predicate, object_, None)
                ):
                    yield (s1, p1, o1), (c for c in matchingCtxs)
        else:
            for (s1, p1, o1), cg in self.storage.triples(
                (subject, predicate, object_), context
            ):
                yield (s1, p1, o1), cg

## `` REGEXTerm

    REGEXTerm(expr)

Bases: `str`

REGEXTerm can be used in any term slot and is interpreted as a request to perform a REGEX match (not a string comparison) using the value (pre-compiled) for checking rdf:type matches

Methods:

- `__reduce__` –

Attributes:

- `compiledExpr` –

Source code in `rdflib/plugins/stores/regexmatching.py`

    29
    30

    def __init__(self, expr):
        self.compiledExpr = re.compile(expr)

### `` compiledExpr `instance-attribute`

    compiledExpr = compile(expr)

### `` __reduce__

    __reduce__()

Source code in `rdflib/plugins/stores/regexmatching.py`

    32
    33

    def __reduce__(self):
        return (REGEXTerm, ("",))

## `` regexCompareQuad

    regexCompareQuad(quad, regexQuad)

Source code in `rdflib/plugins/stores/regexmatching.py`

    36
    37
    38
    39
    40
    41
    42

    def regexCompareQuad(quad, regexQuad):  # noqa: N802, N803
        for index in range(4):
            if isinstance(regexQuad[index], REGEXTerm) and not regexQuad[
                index
            ].compiledExpr.match(quad[index]):
                return False
        return True