Python 2.7 help

Joined
Feb 3, 2007
Messages
3,783
So I'm trying to use re to tokenize an infix expression so I can get a stack I can work with for infix-to-postfix conversion.

However, I keep on running into a problem:
Code:
>>> import re
]>>> expression = '11*(12 + 13)–14/15'
>>> re.findall('[0-9]+|[+-/()]|\*\*?', expression)
['11', '*', '(', '12', '+', '13', ')', '14', '/', '15']]
If you don't see it, it's that '-' (hyphen/"minus sign") won't register. What am I doing wrong?

edit: I guess all python-specific questions can go in this thread?
edit2: I'm confused, it goes from working to not-working:
Spoiler :
Code:
def tokenize(expression):
    import re
    return re.findall('[0-9]+|[-+/()]|\*\*?', expression)

>>> tokenize('11*(12 + 13)–14/15')
['11', '*', '(', '12', '+', '13', ')', '14', '/', '15']
>>> tokenize('11*(12 + 13)–14/15')
['11', '*', '(', '12', '+', '13', ')', '14', '/', '15']
>>> tokenize('11*(12 + 13)–14/15')
['11', '*', '(', '12', '+', '13', ')', '14', '/', '15']
>>> tokenize('11*(12 + 13)+14/15')
['11', '*', '(', '12', '+', '13', ')', '+', '14', '/', '15']
>>> re.findall('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)+14/15')

Traceback (most recent call last):
  File "<pyshell#191>", line 1, in <module>
    re.findall('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)+14/15')
NameError: name 're' is not defined
>>> import re
>>> re.findall('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)+14/15')
['11', '*', '(', '12', '+', '13', ')', '+', '14', '/', '15']
>>> re.find('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)+14/15')

Traceback (most recent call last):
  File "<pyshell#194>", line 1, in <module>
    re.find('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)+14/15')
AttributeError: 'module' object has no attribute 'find'
>>> re.findall('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)+14/15')
['11', '*', '(', '12', '+', '13', ')', '+', '14', '/', '15']
>>> re.findall('[0-9]+|[-+/()]|\*\*?', '11*(12 + 13)-14/15')
['11', '*', '(', '12', '+', '13', ')', '-', '14', '/', '15']
>>> tokenize('11*(12 + 13)-14/15')
['11', '*', '(', '12', '+', '13', ')', '-', '14', '/', '15']
>>> tokenize('11*(12 + 13)-14/15') [B][COLOR="Red"]#works[/COLOR][/B]
['11', '*', '(', '12', '+', '13', ')', '-', '14', '/', '15']
>>> tokenize('11*(12 + 13)&#8211;14/15') [B][COLOR="Red"]#no work[/COLOR][/B]
['11', '*', '(', '12', '+', '13', ')', '14', '/', '15']
>>>
edit3: figured it out Ahahaha, I was editing this post when I realized that one was using an ACTUAL MINUS SIGN (or some dash, either way it's a bit longer than the hyphen) while the other was using a hyphen :lol:
 
Back
Top Bottom