help with Or

2005-05-30
2013-05-14
  • Steven Siew
    Steven Siew
    2005-05-30

    Is there a way to simplifiy this

    singledigit = Or([Literal('0'),Literal('1'),Literal('2'),Literal('3'),Literal('4'),Literal('5'),Literal('6'),Literal('7'),Literal('8'),Literal('9')])

     
    • Paul McGuire
      Paul McGuire
      2005-05-30

      oneOf is a short cut for doing this kind of Or'ing, with a couple of advantages.  For one thing, Or will evaluate all alternative paths, and choose the longest match.  So this Or will try to match every digit, even if it has already matched '0' or '1'.  So MatchFirst would be preferable here.  oneOf does this automatically, splitting on whitespace.  It would look like:
      singleDigit = oneOf("0 1 2 3 4 5 6 7 8 9")

      But you might find this alterative to run a bit faster.  Define a Word that is exactly one character long, that can be composed of any digit.  Using the Word class, this looks like:
      singleDigit = Word("0123456789",exact=1)

      Either will work, but the second will run faster.

      -- Paul