summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2019-10-13 20:18:46 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2019-10-13 20:18:46 -0500
commitf91b27fed996060b1189325cc12097b0a985e012 (patch)
tree60185dde6d7cddb5ccfb779b1a370b685e617acd
parentf5ffaa2ab8e67668604c2542cead207a5d3110cf (diff)
downloadpyparsing-git-f91b27fed996060b1189325cc12097b0a985e012.tar.gz
Backport support for 3rd party regex module
-rw-r--r--CHANGES7
-rw-r--r--pyparsing.py30
2 files changed, 28 insertions, 9 deletions
diff --git a/CHANGES b/CHANGES
index 1c1be35..aa2a073 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,15 +2,16 @@
Change Log
==========
-Version 2.4.3 - September, 2019
--------------------------------
+Version 2.4.3 - October, 2019
+-----------------------------
- Fixed a bug in ParserElement.__eq__ that would for some parsers
create a recursion error at parser definition time. Thanks to
Michael Clerx for the assist. (Addresses issue #123)
- Backports from pyparsing 3.0.0:
- . __diag__.enable_all_warnings().
+ . __diag__.enable_all_warnings()
. Fixed bug in PrecededBy which caused infinite recursion, issue #127
+ . support for using regex-compiled RE to construct Regex expressions
Version 2.4.2 - July, 2019
diff --git a/pyparsing.py b/pyparsing.py
index 17e0bdf..6a8a925 100644
--- a/pyparsing.py
+++ b/pyparsing.py
@@ -96,7 +96,7 @@ classes inherit from. Use the docstrings for examples of how to:
"""
__version__ = "2.4.3"
-__versionTime__ = "25 Sep 2019 23:51 UTC"
+__versionTime__ = "13 Oct 2019 19:20 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -212,7 +212,7 @@ __all__ = ['__version__', '__versionTime__', '__author__', '__compat__', '__diag
'stringStart', 'traceParseAction', 'unicodeString', 'upcaseTokens', 'withAttribute',
'indentedBlock', 'originalTextFor', 'ungroup', 'infixNotation', 'locatedExpr', 'withClass',
'CloseMatch', 'tokenMap', 'pyparsing_common', 'pyparsing_unicode', 'unicode_set',
- 'conditionAsParseAction',
+ 'conditionAsParseAction', 're',
]
system_version = tuple(sys.version_info)[:3]
@@ -3256,14 +3256,32 @@ class Regex(Token):
If the given regex contains named groups (defined using ``(?P<name>...)``),
these will be preserved as named parse results.
+ If instead of the Python stdlib re module you wish to use a different RE module
+ (such as the `regex` module), you can replace it by either building your
+ Regex object with a compiled RE that was compiled using regex, or by replacing
+ the imported `re` module in pyparsing with the `regex` module:
+
+
Example::
realnum = Regex(r"[+-]?\d+\.\d*")
date = Regex(r'(?P<year>\d{4})-(?P<month>\d\d?)-(?P<day>\d\d?)')
# ref: https://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression
roman = Regex(r"M{0,4}(CM|CD|D?{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})")
+
+ import regex
+ parser = pp.Regex(regex.compile(r'[0-9]'))
+
+ # or
+
+ import pyparsing
+ pyparsing.re = regex
+
+ # both of these will use the regex module to compile their internal re's
+ parser = pp.Regex(r'[0-9]')
+ parser = pp.Word(pp.nums)
+
"""
- compiledREtype = type(re.compile("[A-Z]"))
def __init__(self, pattern, flags=0, asGroupList=False, asMatch=False):
"""The parameters ``pattern`` and ``flags`` are passed
to the ``re.compile()`` function as-is. See the Python
@@ -3288,13 +3306,13 @@ class Regex(Token):
SyntaxWarning, stacklevel=2)
raise
- elif isinstance(pattern, Regex.compiledREtype):
+ elif hasattr(pattern, 'pattern') and hasattr(pattern, 'match'):
self.re = pattern
- self.pattern = self.reString = str(pattern)
+ self.pattern = self.reString = pattern.pattern
self.flags = flags
else:
- raise ValueError("Regex may only be constructed with a string or a compiled RE object")
+ raise TypeError("Regex may only be constructed with a string or a compiled RE object")
self.re_match = self.re.match