shashuec
shashuec

Reputation: 694

Regex in Python

I am new to python and want to run a code in Python.
Code that I have written is

#!/usr/bin/python

import sys
import re

for line in sys.stdin:
    results = re.search('ipAddress=([^&]*])', line)
    if len(results.group(1)) != 0:
        print results.group(1)

Requirement is finding IP address in a string.String will be something like this

blah....ipAddress=173.110.208.118&deviceId

Output: 173.110.208.118

Please let me know what I am doing wrong..

Upvotes: 3

Views: 6493

Answers (4)

eyquem
eyquem

Reputation: 27585

Any specific reason for not using regex?

No, there's no need of a more complicated tool. If your string has a strongly repeatitive pattern, even regex are not absolutely required:

ch = 'blah....ipAddress=173.110.208.118&deviceId'


print ch.split('=')[1].split('&')[0]

print ch.split('=')[1][0:15]

print ch[-34:].strip('ipAddress=&deviceId')

result

173.110.208.118
173.110.208.118
173.110.208.118

Upvotes: 1

hdima
hdima

Reputation: 3637

Your input looks like URL query string. Don't parse it by regexp, consider to use urlparse.parse_qs or urlparse.parse_qsl (for Python version less than 2.6 use cgi.parse_qs or cgi.parse_qsl respectively). Those functions hide many low level details, for example handling of URL encoded characters.

Example code for urlparse.parse_qs:

>>> from urlparse import parse_qs
>>> query = parse_qs("ipAddress=173.110.208.118&deviceId=Some%20Id")
>>> query["ipAddress"]
['173.110.208.118']
>>> query["deviceId"]
['Some Id']

In urlparse module you can also find functions for extracting query string from a full URL.

Upvotes: 4

Conrad.Dean
Conrad.Dean

Reputation: 4421

If you're paranoid about regex being too greedy you can use this one to explicitly match the contents like xxx.xxx.xxx.xxx

>import re
>s = "blah...ipAddress=173.110.208.118&deviceid"
>results = re.search('ipAddress=(\d+\.\d+\.\d+\.\d+)',s)
>results.groups()

('173.110.208.118',)

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

I think you've got one ] too many there. Try

results = re.search('ipAddress=([^&]*)', line)

Upvotes: 6

Related Questions