Python regular expression help

Posted by dlw on Stack Overflow See other posts from Stack Overflow or by dlw
Published on 2010-06-11T01:06:24Z Indexed on 2010/06/11 1:12 UTC
Read the original article Hit count: 303

Filed under:
|

Hi SO,

I can't seem to create the correct regular expression to extract the correct tokens from my string. Padding the beginning of the string with a space generates the correct output, but seems less than optimal:

>>> import re
>>> s = '-edge_0triggered a-b | -level_Sensitive c-d | a-b-c'
>>> re.findall(r'\W(-[\w_]+)',' '+s)
['-edge_0triggered', '-level_Sensitive'] # correct output

Here are some of the regular expressions I've tried, does anyone have a regex suggestion that doesn't involve changing the original string and generates the correct output

>>> re.findall(r'(-[\w_]+)',s)
['-edge_0triggered', '-b', '-level_Sensitive', '-d', '-b', '-c']
>>> re.findall(r'\W(-[\w_]+)',s)
['-level_Sensitive']

Thanks -- DW

© Stack Overflow or respective owner

Related posts about python

Related posts about regex