Python extract substring from string

时间:2016-07-11 21:24:53

标签: python

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 str(dict).splice('\n')[index], 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 whois from subprocess.call, yet would like to figure out how to do it in python. i know that i can grab part of it with re.configure, yet again the return changes so re.compile 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

thanks, em

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

我最终做的是

domain = socket.gethostbyname(hostname/ipaddr) #if ipaddr it stays the same
email_addr = re.search(r'(\w*\D\w*@\w*\.\w*)', IPWhois(domain).lookup_whois()['nets'][0]['emails']).group(1)

我不得不把额外的\w*\D放在那里,因为有些电子邮件地址中有一个连字符。我正在查看正则表达式而不是完整的库。