2

I've been through every post I could find on the subject and nothing have answered my problem;

This is the code:
output = 'Scan results for BrainS (192.168.43.111) Scan results for Slave (192.168.43.107) Scan results for SlaveSmall (192.168.43.242)' while (True) if output[i].isdigit(): # i has initial value of 15, j=0 f[j]=output[i] j+=1 i+=1 elsif(i == len(output)): break else: i+=1 continue

Print:
>>>f ['1','9','2','1','6','8','4','3','1','1','1','0','0','0']

As you can see I'm trying to extract the IP as it is with dots(in this code I didnt try to extract the dots but only the numbers), I cant figure out how do get the string I want exactly as it is: f = 192.168.43.111

Any suggestions? better commands?

1

2 Answers 2

0

For multiple pairs of parenthesis in the string I think it is best to use a regex like so:

import re

output = '''Scan results for BrainS (192.168.43.111) 
            Scan results for Slave (192.168.43.107)
            Scan results for SlaveSmall (192.168.43.242)'''

f = re.findall(r'\(([^()]+)\)',output)
>>>['192.168.43.111', '192.168.43.107', '192.168.43.242']

Try it here!

Sign up to request clarification or add additional context in comments.

4 Comments

wow thanks a lot works like a charm,how about if there were several lines like this one? but different IP's. then i'd have a few "(" & ")" , is there a solution to that?
Edit your question to show these other lines. The above code only works if there is only one pair of parenthesis in the string.
I've just edited it, I've added the actual output I get, I tried to simplify it before. I hope this can help you understand my question better.
That should work @eyal360 , the earlier answer only makes sense for one pair of parenthesis :)
0

Here you go, this code will do the job!

output = 'My computer IP is = (192.168.43.111) and yours not.'
ip=[]
ip_flag = False

for x in output:
    if x == '(':
        ip_flag = True
        continue
    if ip_flag:
        if x != ')':
            if x != '.': # remove this line of you want the dots
                ip.append(x)
        else:
            break
print(ip)
theIp="" 
for i in ip: 
    theIp+=i

3 Comments

This answer actually gave me the ['1','9','2',...] formation that I didnt want in the first place, but it works good.
@eyal360 if you want the dots remove this line if x != '.': if you want a string just add a loop at the end like this theIp="" for i in ip: theIp+=i done
you can't see it because im under 15 Rep , but you got it, its just not shown. [link]s30.postimg.org/8ci2mzhfl/Upvote.jpg

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.