RegEx question

Tim Chase python.list at tim.thechases.com
Thu Oct 4 16:05:25 EDT 2007


>>> try @param\[(in|out)\] \w+
>>>
>> This didn't work either :(
>>
>> The tool using this regular expression (Comment Reflower for VS2005) May be
>> broken...
> 
> How about @param\[[i|o][n|u]t*\]\w+ ?

...if you want to accept patterns like

   @param[iutttttt]xxxxxxx

...

The regexp at the top (Adam's original reply) would be the valid 
regexp in python and matches all the tests thrown at it, assuming 
it's placed in a raw string:

   r = re.compile(r"@param\[(in|out)\] \w+")

If it's not a python regexp, this isn't really the list for the 
question, is it? ;)

However, things to try:

- sometimes the grouping parens need to be escaped with "\"

- sometimes "\w" isn't a valid character class, so use the 
long-hand variant of something like "[a-zA-Z0-9_]]

- sometimes the "+" is escaped with a "\"

- if you don't use raw strings, you'll need to escape your "\" 
characters, making each instance "\\"

HTH,

-tkc





More information about the Python-list mailing list