One more regular expressions question

Peter Otten __peter__ at web.de
Thu Jan 18 05:51:48 EST 2007


Victor Polukcht wrote:

> I have a couple of strings like:
> 
> Unassigned Number (1)                                    32
> No Route To Destination (3)                              12
> Normal call clearing (16)                                   2654
> User busy (17)                                                 630
> No user respond (18)                                        5
> User alerting no answer (19)                             16
> Call rejected (21)                                             3
> Destination out of order (27)                             1
> Invalid number format (28)                                32
> Normal unspecified (31)                                   32
> No channel available (34)                                 2
> Temporary failure (41)                                      11
> Switching equipment congestion (42)                4
> Resource unavailable unspecified (47)               2
> Bearer capability not authorized (57)                 73
> Incomp. dest. / Non-existent CUG (88)              1
> Recovery on timer expiry (102)                          2
> 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.

The following solution is 100% regex-free :-)

>>> print lines
Unassigned Number (1)                                    32
No Route To Destination (3)                              12
Normal call clearing (16)                                   2654
User busy (17)                                                 630
No user respond (18)                                        5
User alerting no answer (19)                             16
Call rejected (21)                                             3
Destination out of order (27)                             1
Invalid number format (28)                                32
Normal unspecified (31)                                   32
No channel available (34)                                 2
Temporary failure (41)                                      11
Switching equipment congestion (42)                4
Resource unavailable unspecified (47)               2
Bearer capability not authorized (57)                 73
Incomp. dest. / Non-existent CUG (88)              1
Recovery on timer expiry (102)                          2
Interworking, unspecified (127)                          5

>>> [int(line.rsplit("(", 1)[1].split(")", 1)[0]) for line in
lines.splitlines()]
[1, 3, 16, 17, 18, 19, 21, 27, 28, 31, 34, 41, 42, 47, 57, 88, 102, 127]

Peter



More information about the Python-list mailing list