3

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..

0

4 Answers 4

6

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

results = re.search('ipAddress=([^&]*)', line)
Sign up to request clarification or add additional context in comments.

Comments

4

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.

2 Comments

I wrote python code first time.I think regex is working fine me.Any specific reason for not using regex?
For very simple tasks like extracting IPs from bunch of a lines it's OK to use regexp. But in this case you can also use one line AWK code like this: awk '/ipAddress=/ {print $2}' FS='ipAddress=|&' < lines.txt. If you learn Python it's important to know about most frequently used modules in the standard library.
1

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',)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.