One more regular expressions question

Victor Polukcht vpolukcht at gmail.com
Thu Jan 18 13:00:09 CET 2007


Great thanks.

You post helped me so much!

My resulting regexp is:
"(?P<var1>^(.*)\s*)\(((?P<var2>\d+))\)\s+((?P<var3>\d+))"

On Jan 18, 2:38 pm, "Daniele Varrazzo" <daniele.varra... at gmail.com>
wrote:
> Victor Polukcht wrote:
> > I have a couple of strings like:
>
> > Unassigned Number (1)                                    32
> [...]
> > Interworking, unspecified (127)                          5
>
> > I need to get:
> > Error code (value in brackets) - Value - Message.
>
> > My actual problem is i can't get how to include space, comma, slash.Probably you have some escaping problem. The substitution:
>
>   re.sub(r"^(.*)\s*\((\d+)\)\s+(\d+)", r'\2 - \3 - \1', row)
>
> does the required job (where "row" is one of your lines)
>
> To match a special character, such as "(", you need to escape it with a
> "\", because it has a special meaning in the regexp syntax. Because "\"
> is the escaping mechanism for Python strings too, you better use raw
> strings to specify the pattern.
>
> Other special character/groups matching patterns, such as "\s" to
> specify whitespaces, are documented, together with everything else you
> need, athttp://docs.python.org/lib/re-syntax.html
> 
> HTH
> 
> Daniele




More information about the Python-list mailing list