summaryrefslogtreecommitdiff
path: root/src/examples/invRegex.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2016-01-27 23:42:22 +0000
committerPaul McGuire <ptmcg@austin.rr.com>2016-01-27 23:42:22 +0000
commit986c535fca52ce0793ce7848c506b49f95126120 (patch)
tree3c763df22c93908c5d9004c018a5986d06f35646 /src/examples/invRegex.py
parentfb611ffab05e3fc1d4d3b7dbe2d4905c0d63fcf4 (diff)
downloadpyparsing-git-986c535fca52ce0793ce7848c506b49f95126120.tar.gz
Cleanup examples, fix typo in fourFn.py, convert sample tests to use runTests
Diffstat (limited to 'src/examples/invRegex.py')
-rw-r--r--src/examples/invRegex.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/examples/invRegex.py b/src/examples/invRegex.py
index 965fe8f..26d12ed 100644
--- a/src/examples/invRegex.py
+++ b/src/examples/invRegex.py
@@ -150,7 +150,7 @@ def parser():
global _parser
if _parser is None:
ParserElement.setDefaultWhitespaceChars("")
- lbrack,rbrack,lbrace,rbrace,lparen,rparen = map(Literal,"[]{}()")
+ lbrack,rbrack,lbrace,rbrace,lparen,rparen,colon,qmark = map(Literal,"[]{}():?")
reMacro = Combine("\\" + oneOf(list("dws")))
escapedChar = ~reMacro + Combine("\\" + oneOf(list(printables)))
@@ -158,6 +158,7 @@ def parser():
reRange = Combine(lbrack + SkipTo(rbrack,ignore=escapedChar) + rbrack)
reLiteral = ( escapedChar | oneOf(list(reLiteralChar)) )
+ reNonCaptureGroup = Suppress("?:")
reDot = Literal(".")
repetition = (
( lbrace + Word(nums).setResultsName("count") + rbrace ) |
@@ -170,7 +171,7 @@ def parser():
reMacro.setParseAction(handleMacro)
reDot.setParseAction(handleDot)
- reTerm = ( reLiteral | reRange | reMacro | reDot )
+ reTerm = ( reLiteral | reRange | reMacro | reDot | reNonCaptureGroup)
reExpr = operatorPrecedence( reTerm,
[
(repetition, 1, opAssoc.LEFT, handleRepetition),
@@ -184,10 +185,7 @@ def parser():
def count(gen):
"""Simple function to count the number of elements returned by a generator."""
- i = 0
- for s in gen:
- i += 1
- return i
+ return sum(1 for _ in gen)
def invert(regex):
"""Call this routine as a generator to return all the strings that
@@ -212,6 +210,7 @@ def main():
fooba[rz]{2}
(foobar){2}
([01]\d)|(2[0-5])
+ (?:[01]\d)|(2[0-5])
([01]\d\d)|(2[0-4]\d)|(25[0-5])
[A-C]{1,2}
[A-C]{0,3}
@@ -229,6 +228,9 @@ def main():
A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|S[bcegimnr]?|T[abcehilm]|Uu[bhopqst]|U|V|W|Xe|Yb?|Z[nr]
(a|b)|(x|y)
(a|b) (x|y)
+ [ABCDEFG](?:#|##|b|bb)?(?:maj|min|m|sus|aug|dim)?[0-9]?(?:/[ABCDEFG](?:#|##|b|bb)?)?
+ (Fri|Mon|S(atur|un)|T(hur|ue)s|Wednes)day
+ A(pril|ugust)|((Dec|Nov|Sept)em|Octo)ber|(Febr|Jan)uary|Ju(ly|ne)|Ma(rch|y)
""".split('\n')
for t in tests:
@@ -237,14 +239,19 @@ def main():
print('-'*50)
print(t)
try:
- print(count(invert(t)))
+ num = count(invert(t))
+ print(num)
+ maxprint = 30
for s in invert(t):
print(s)
+ maxprint -= 1
+ if not maxprint:
+ break
except ParseFatalException as pfe:
print(pfe.msg)
- print()
+ print('')
continue
- print()
+ print('')
if __name__ == "__main__":
main()