One more regular expressions question

Daniele Varrazzo daniele.varrazzo at gmail.com
Thu Jan 18 06:38:37 EST 2007


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, at http://docs.python.org/lib/re-syntax.html

HTH

Daniele




More information about the Python-list mailing list