Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in Python

Tags:

python

regex

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

like image 475
shashuec Avatar asked Feb 04 '26 02:02

shashuec


1 Answers

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

results = re.search('ipAddress=([^&]*)', line)
like image 100
Tim Pietzcker Avatar answered Feb 06 '26 16:02

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!