emetib emetib - 3 months ago 13x
Python Question

Python extract substring from string

so i'm trying to convert a bash script, that i wrote, into python, that i'm learning, and the python equivalent of the bash whois just can't give me the answer that i need.

this is what i have in bash-

whois 'ip address' | grep -i abuse | \
grep -o [[:alnum:]]*\@[[:alnum:]]*\.[[:alpha:]]* | sort -u

and it works perfectly.

when trying to do something similar in python(3.5.2)-

IPWhois('ip address').lookup_whois()

it's giving me a dictionary with the object that i'm looking for in the first value about half way through the string.

i have tried to put it into
, yet with each iteration the index changes so i can't put it into a script like that. also the bash whois can do both ip addresses and domain names with out having to convert.
i think that i have figured out the conversion, yet trying to grab the results from the IPWhois is giving me a pain in the butt.

i could call the bash
, yet would like to figure out how to do it in python. i know that i can grab part of it with
, yet again the return changes so
would have to change each time also.

do i keep trying or do i just stick with the bash script that works so well?
i have already written most of the python script and the things that i have to look up are helping me learn.

any ideas?

you can see the bash script here



You were on the right track with using the regex module. Your search could be

re.search(r'(\w*@\w*\.\w*)', IPWhois(ip).lookup_whois()['nets'][0]['emails']).group(1)