Skip to content

Commit

Permalink
Add chain feature to emulate repeated capture groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Jan 25, 2016
1 parent e380229 commit 6f7abd5
Show file tree
Hide file tree
Showing 6 changed files with 742 additions and 15 deletions.
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ You can also return a dict of keywords arguments for ``Match`` object.
You can define several patterns with a single ``functional`` method call, and function used can return multiple
matches.

Chain Patterns
--------------
Chain Patterns are ordered composition of string, functional and regex patterns. Repeater can be set to define
repetition on chain part.

.. code-block:: python
>>> r = Rebulk().chain(children=True, formatter={'episode': int, 'version': int}, flags=re.IGNORECASE)\
... .regex(r'e(?P<episode>\d{1,4})').repeater(1)\
... .regex(r'v(?P<version>\d+)').repeater('?')\
... .regex(r'[ex-](?P<episode>\d{1,4})').repeater('*')\
... .close() # .repeater(1) could be omitted as it's the default behavior
>>> r.matches("This is E14v2-15-16-17").to_dict(implicit=True) # converts matches to dict and keep multiple values
MatchesDict([('episode', [14, 15, 16, 17]), ('version', 2)])
Patterns parameters
-------------------
Expand Down
Loading

0 comments on commit 6f7abd5

Please sign in to comment.