Skip to main content

Regular Expression with CHain Iteration

Project description

rechi

Regular Expression with CHain Iteration

Installation

Install via pip:

pip install rechi

Usage

Using like re

Import rechi as re and use it like built-in re:

>>> import rechi as re

compile() a pattern and use match() to find matches similar to re: Note: currently only compile() and match() are supported.

match() returns a list of re.Match objects.

>>> p = re.compile("a(b|B)c")
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>]
>>> m[0].groups()
('b')

Chaining

In addition, you can chain patterns together. A chain maches if all patterns in the chain match in that given order until the last pattern in the chain.

>>> p = re.compile("a(b|B)c").chain("(d)").chain("(e)")
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 4), match='d'>, <re.Match object; span=(4, 5), match='e'>]
>>> m[0].groups()
('b',)
>>> m[1].groups()
('d',)
>>> m[2].groups()
('e',)

Using options

You can define multiple options in the chain by using a list:

>>> p = re.compile("a(b|B)c").chain(["(d)", "(D)"])
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 4)]
>>> m[0].groups()
('b',)
>>> m[1].groups()
('d',)
>>> m = p.match("abcDe")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 4)]
>>> m[0].groups()
('b',)
>>> m[1].groups()
('D',)

To define an optional pattern, use None as the pattern:

>>> p = re.compile("a(b|B)c").chain(["(d)", None])
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 4)]
>>> m[0].groups()
('b',)
>>> m[1].groups()
('d',)
>>> m = p.match("abce")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>]
>>> m[0].groups()
('b',)

Note: be careful with using None as the last pattern in the chain as it will always match and the other options will not be checked:

>>> p = re.compile("a(b|B)c").chain([None, "(d)"])
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>]
>>> m[0].groups()
('b',)

Chaining with recursion

You can also chain patterns with recursion. Since a chain matches if all its elemenets matched, having recursion cannot match any string. To resolve this problem, you need to close the chain with a None pattern.

>>> p = re.compile("a(b|B)c")
>>> p = p.chain([p, None])  # Will match any number of p pattern. Don't forget to add None as an option to allow match
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>]
>>> m = p.match("abcaBcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 6), match='aBc'>]
>>> m = p.match("abcaBcabcaBcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 6), match='aBc'>, <re.Match object; span=(6, 9), match='abc'>, <re.Match object; span=(9, 12), match='aBc'>]

Note that without adding the None pattern, the chain will not be able to terminate thus will never match.

Limiting the number of matches

You can also limit the maximum number of match allowed in a pattern object:

>>> p = re.compile("a(b|B)c", max=3)
>>> p = p.chain([p, None])  # Adding None as optional pattern to be able to terminate the chain
>>> m = p.match("abcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>]
>>> m = p.match("abcaBcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 6), match='aBc'>]
>>> m = p.match("abcaBcabcaBcde")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 6), match='aBc'>, <re.Match object; span=(6, 9), match='abc'>]

Nesting

You can also nest patterns to create complex options and chains:

>>> p = re.compile("a(b|B)c").chain([re.compile("d(e|E)f").chain("(g)"), "(D)"])
>>> m = p.match("abcdefg")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 6), match='def'>, <re.Match object; span=(6, 7), match='g'>]
>>> m[0].groups()
('b',)
>>> m[1].groups()
('e',)
>>> m[2].groups()
('g',)
>>> m = p.match("abcDefg")
>>> print(m)
[<re.Match object; span=(0, 3), match='abc'>, <re.Match object; span=(3, 4), match='d'>]
>>> m[0].groups()
('b',)
>>> m[1].groups()
('D',)

Functions

compile

def compile(pattern, flags=0, max=None)

Compile a pattern and return a pattern object.

  • pattern: string, Pattern, None, or list of the above.
  • flags: re flags to use to compile patterns, default 0.
  • max: maximum number of matches allowed, default None.

Returns: Pattern object.

Pattern class

class Pattern
    def __init__(self, pattern, flags=0, max=None)
    def chain(self, patterns)
    def match(self, string, pos=0, endpos=None)

    @property
    def flags(self)
    @property
    def pattern(self)
    @property
    def next(self)
    @property
    def max(self)

Pattern.chain

def chain(self, patterns)

Chain patterns to the end of the chain.

pattern: string, Pattern, None, or list of the above.

Returns: Pattern object. Note: the head of the chain is returned.

Pattern.match

def match(self, string, pos=0, endpos=None)

Match a string against the pattern.

  • string: string to match.
  • pos: start position, default 0.
  • endpos: end position, default None.

Returns: list of re.Match objects or None if no match.

Pattern.flags

@property
def flags(self)

Get the flags used to compile the pattern.

Pattern.pattern

@property
def pattern(self)

Get the list of patterns within the pattern object.

Pattern.next

@property
def next(self)

Get the next pattern in the chain.

Pattern.max

@property
def max(self)

Get the maximum number of matches allowed.

License

GPL-3.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rechi-1.0.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rechi-1.0.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file rechi-1.0.0.tar.gz.

File metadata

  • Download URL: rechi-1.0.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for rechi-1.0.0.tar.gz
Algorithm Hash digest
SHA256 033e8fd7dbc22505be6c3dab1b624b11b431fa7f4f6add140582c9bdb11be24f
MD5 329d64985c843988a0871bde16196131
BLAKE2b-256 2b0322df1af78efa0f1fe09bf986f33635ad40076fc59fd0e235146632801767

See more details on using hashes here.

File details

Details for the file rechi-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: rechi-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for rechi-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d04385e00e7010400ebeb6f1d909b63413ce266840a1da32f32ad9925c925cc1
MD5 ad6c8027a1e473442fa739389238bd45
BLAKE2b-256 904c3aab501831370d4fbd6cd87dd4db14d00e69f4071e1fd56492af964583ac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page