faqts : Computers : Programming : Languages : Python : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 6 people (67%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

grouping in regular expressions

Jul 18th, 2000 21:16
unknown unknown, Wolfgang Strobl


Problem:
I am finding it difficult to match the ip address in the
following line using re module.
"       inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0"
Solution:
>>> import re
>>> s="       inet addr:192.168.0.1  Bcast:192.168.0.255 
Mask:255.255.255.0"
>>> p=re.compile('\s+inet addr:(\d+\.\d+\.\d+\.\d+)\s+.*')
>>> p.match(s).groups()
    ('192.168.0.1',)
>>>