## [Pyparsing] How to distinguish a variable from a integer

 [Pyparsing] How to distinguish a variable from a integer From: Gustavo Narea - 2009-05-14 17:02:03 ```Hello, everybody. First of all, I wanted to thank you for this awesome package. I'm having fun with it. :) I've read O'reilly's shortcut on Pyparsing, but still I can't find an answer to this: One of the components of the grammar I'm defining is an operand. Operands can be a number or a variable. A variable is a string made up of word characters (in any language), numbers (in any language/culture) and/or a spacing character (underscores by default). I'm using the following: """ import re from pyparsing import * # Defining the numbers: decimal_sep = Literal(".") decimals = Optional(decimal_sep + OneOrMore(Word(nums))) number = Combine(Word(nums) + decimals) # Defining the variables: variable = Regex("[\w\d_]+", re.UNICODE) # Finally, let's define the operand: operand = number | variable """ The operand above works perfectly with the following expressions: hello -> variable 23 -> number hello_world -> variable But it doesn't support variables which begin with a number (e.g., "1st_variable"). I get the following exception all the time: >>>> from varnums import * >>>> operand.parseString("1st_variable") >(['1'], {}) >>>> operand.parseString("1st_variable", parseAll=True) >Traceback (most recent call last): > File "", line 1, in > File > "/home/gustavo/System/pyenvs/booleano/lib/python2.6/site-packages/pyparsing >-1.5.2-py2.6.egg/pyparsing.py", line 1076, in parseString raise exc >pyparsing.ParseException: Expected end of text (at char 1), (line:1, col:2) I know I can invert the definition of the operand (i.e., "operand = variable | number"), but then strings like "22" will be matched as variables (not numbers). How can I fix this? Thanks in advance. -- Gustavo Narea ;. | Tech blog: =Gustavo/(+blog)/tech ~ About me: =Gustavo/about | ```