Find IP in text ?

Just starting out? Need help? Post your questions and find answers here.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Kaeru Gaman wrote:AND talked about OpenNetworkConnection...
how should I know it expects a string? I never used it.
Excuse me, I'm afk for a while => laughing :lol:

Of course, this topic is about the IP, not about network programming.
But you can just look at the manual, then you would have seen that I'm right.
And yes, in future, you should read each post twice, as I already said that OpenNetworkConnection() expects a string. And if the thread starter talks about his IRC bot and that he needs to connect to the IP returned by our procedure - it's clear that he needs a string.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

pdwyer wrote:Edit, Sometime IP's are formated like this 139.023.198.123 (with a leading zero) and your expression fails here.
Huh? I never saw an IP with leading zero.

IMHO this is not a valid IP. I just wrote "ping 127.000.000.001" into cmd.exe and it automatically turned the IP into "127.0.0.1".
So, in fact my expression is right.

But if you want to have this support, just use

Code: Select all

((0|1|2)?\d\d?\.){3}(|1|2)?\d\d?
And yes, my expression is faster, because I use the multiplicator. You just wrote [0-9] many times and used more |; the more OR-expressions, the slower the expression.

// Edit:
Your code is wrong, pwdyer!
You just use dot (.). But dot stands for every character, so you have to mask it!
Use \. (masked dot) instead, just like my procedure.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

XXX
Last edited by Kaeru Gaman on Mon Oct 13, 2008 6:59 pm, edited 1 time in total.
oh... and have a nice day.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: Find IP in text ?

Post by pdwyer »

AND51, what are you on? :roll:

Just because you haven't seen the zero format it's doesn't exist? "Your humble opinion" on this counts for nothing! It exists, I've seen it, end of story. You will notice that with ping 127.000.000.001 it works though, ie its valid!

Yours is faster? okay, congrates, you're competing with someone who's never never used regular expressions before.

Thanks for the info about the dot, I didn't know that. offensively put but useful none the less

For your comments to KG, about reading the question twice... The posted question was
elfo116 wrote:Some body can help me please ?
I need a procedure to find a IP in a text file and procedure return will be String.
Is there some pedantic, childish competitive nature to you that you feel the needs to post in ways that are demeaning? Got self confidence issues do you and need to blow your own trumpet at others expense? :?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

AND51 wrote:
pdwyer wrote:Edit, Sometime IP's are formated like this 139.023.198.123 (with a leading zero) and your expression fails here.
Huh? I never saw an IP with leading zero.
Just start your nintendo wii and configure it to have WLAN. It will generate leading zeros.
bye,
Daniel
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

Another approach. It also works when there is more than one IP address in the text (PB 4.20).

//edit: New improved code.
//edit: Fixed a bug.

Code: Select all

EnableExplicit

#IPchars = "0123456789."

Procedure.s IPAddress (text.s, begin=0)
   Static start
   Protected posn, i, v, ok, token.s, s.s

   If begin > 0
      start = begin
   EndIf

   While start <= Len(text)
      While start <= Len(text) And FindString(#IPchars, Mid(text, start, 1), 1) = 0
         start + 1
      Wend
      posn = start
      While posn <= Len(text) And FindString(#IPchars, Mid(text, posn, 1), 1) <> 0
         posn + 1
      Wend
   
      token = Mid(text, start, posn-start)
      start = posn
   
      If CountString(token, ".") = 3
         ok = #True
         For i = 1 To 4
            s = StringField(token, i, ".")
            If s = ""
               ok = #False
               Break
            EndIf
            v = Val(s)
            If v < 0 Or v > 255
               ok = #False
               Break
            EndIf
         Next
         If ok
            ProcedureReturn token
         EndIf
      EndIf
   Wend

   ProcedureReturn ""
EndProcedure


;-- Demo
Define text.s, address.s

text = "Some 15634 foxes jump over 169.45.0.2 lazy dogs on address 169.45.0. Another proper one is 3.145.5.72, while 169.45.0.2.2 is wrong."

address = IPAddress(text, 1)
While address
   Debug address
   address = IPAddress(text)
Wend
Regards, Little John
Last edited by Little John on Mon Oct 13, 2008 2:16 am, edited 3 times in total.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

> why do you always play the ...?
Well, I recommend you to remove this declaration. Otherwise, a mod will do that.

> "..." would be a compliment to you!
This, too of course.

> now it returns an IP found quite reliable
Well, I don't care about your code any longer. So, don't bother me with it, please.

> Just because you haven't seen the zero format it's doesn't exist?
Well, I have never seen this, so what? I read wikipedia, and there is not a single word about leading zeros in IPs.

> ping 127.000.000.001 it works though, ie its valid
This counts... nothing! This might is due the fact that ping.exe offers some kind of correction.
If you write "\\127.00.000.01" into the explorer, you will see that explorer.exe takes this IP without any correction.

> you're competing with someone who's never never used regular expressions before
Well, I saw your expression and I just wanted to help you. Are you lerning-resistent? Sorry, if I wanted to to you a favor.

> Just start your nintendo wii
Yes. I forgot that Nintendo is the creator of IPv4. Thanks.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

AND51 wrote: > now it returns an IP found quite reliable
Well, I don't care about your code any longer. So, don't bother me with it, please.
I thought you read this topic voluntarily? Or did you come here by a mixup of IPs? :o
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

AND51 wrote:> Just start your nintendo wii
Yes. I forgot that Nintendo is the creator of IPv4. Thanks.
No problem :-) . Such a big and wonderful company can't be wrong.
But IPv4 does not define if there's a leading zero or not. IPv4 just describes the binary side and that there are 4 numbers between 0 and 255 with a point in between and the netmasknotation is "ip/bits" or the bits written as ip.
bye,
Daniel
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Find IP in text ?

Post by Kaeru Gaman »

pdwyer wrote:For your comments to KG, about reading the question twice... The posted question was
elfo116 wrote:Some body can help me please ?
I need a procedure to find a IP in a text file and procedure return will be String.
Is there some pedantic, childish competitive nature to you that you feel the needs to post in ways that are demeaning? Got self confidence issues do you and need to blow your own trumpet at others expense? :?
I had to read the question twice because I misinterpreted it at first glance.
I said it was my fault, I know I read too quick sometimes...
some pun at the threadstarter was not intented and I'm sorry if it seemed so.

I just got a bit upset about the way of the other guy.
helping in finding some IP in the middle of some text has nothing to do with network programming.
I don't know anything about it, so I could not get the idea to look at OpenNetworkConnection.
but reading the whole network chapter of the manual was not a condition for helping with some string parsing problem.
oh... and have a nice day.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

DarkDragon wrote:But IPv4 does not define if there's a leading zero or not.
Yes. After re-reading wikipedia, I understand this.
And I said it, that I didn't know this fact before!
But I also thank you for correcting me - that's why forums like this exist.

> I thought you read this topic voluntarily?
Indeed. First, I intended to participiate in here, but interests can change...
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

AND51 wrote:that's why forums like this exist
But forums like this certainly have not been created for posting in ways that are demeaning, and for blowing one's own trumpet at others expense.

Regards, Little John
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Little John wrote:But forums like this certainly have not been created for...
...insulting other members, yes. That's why I say "case closed".

Just give me peace, so I can sleep well tonight — I suggest to delete all tainted posts (mine too, but not that with the RegExp).
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Blah, blah blah, but nobody got a correct solution for this for PB ver 3.94 :roll:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

I don't think that I'm supposed to install PB 3.94, just because elfo116 is using it. It shouldn't be too hard to adapt Kaeru's or my code to PB 3.94.

Regards, Little John
Post Reply