How to find IP address

Hi,

I would like to know if anyone has any code that can find an IP address in a string.
We can have different ways to represent IP, and different way to not be an IP address. for example:

/* aaaah cvcv dfdfsdf df d 100.20.100.255
/* fdf df 10.20.1.1
/* df 05.05.2016 ← this is not IP
/* dfdf 05.05.2016 dfdf dsfs 200.1.20.3.5
/* ABC.DEF.GHI.JKL dfdfsfsd
/* dfsdf dsfsd sdfs sdffhhgh hhg 14.200.33.200
/* jfsdj jdkjfk 20.20…djdf sj
/* dfj sjdflk 10.20.2.200 dkflksdlf asl sdjs

and any other ways…different location in the string…or false cognates…

tks.

try regular expression, it should work for your case

You could probably use EXAMINE … FOR PATTERN … GIVING LOCATION for a starter,
As a pattern (assuming IPv4) you could use ...
This would not end matters however. You would have to isolate the leading and ending digits.
Not to mention checking for numerics, etc.

Hopefully someone in the community has been faced with this problem and written code to cover all contingencies

steve

Hi Steve, thank you, considering that I understood your idea, it didn’t work. when I use pattern ... , the dois between the stars mean that the char will be ignore, and * (star) means any char. tks.

Sorry, forgot about the period being a “skip one” character. Try the following

DEFINE DATA LOCAL
1 #A (A30) INIT <‘123.456.789.12’>
1 #NUM (N2)
1 #REP (A1) INIT <’&’>
END-DEFINE
*
EXAMINE #A FOR “.” REPLACE WITH #REP
EXAMINE #A FOR PATTERN ‘&&&’ GIVING NUMBER #NUM
WRITE ‘=’ #NUM
END

For #REP use anything that is not valid in the alpha string #A; I used the ampersand.

Adding to Steve’s idea, the following is a start. It assumes that the IP would be bordered by a space. If it could be surrounded by parenthesis or other characters, it would need to be adapted. Additional validation may be required (e.g. checking that the numbers are in the proper range, skip reserved IPs, etc.). I’m not sure what your end goal is. I also don’t know if this is the most efficient way but may give some ideas.

DEFINE DATA LOCAL                                         
1 #A (A30) INIT <'abc 199.198.197.196 abc'>               
1 #WORDS (A/*) DYNAMIC                                    
1 #PARTS (A/4) DYNAMIC                                    
1 #C1 (I2)                                                
1 #C2 (I2)                                                
1 #C3 (I2)                                                
END-DEFINE                                                
EXAMINE #A '.' REPLACE ':'                                
EXAMINE #A FULL ' ' NUMBER #C1                            
ADD 1 TO #C1                                              
RESIZE ARRAY #WORDS TO (1:#C1)                            
SEPARATE #A INTO #WORDS (*) WITH DELIMITER ' '            
FOR #C2 = 1 TO #C1                                        
  EXAMINE #WORDS (#C2) PATTERN '*:*:*:*' POSITION #C3     
  IF #C3 = 0                                              
    ESCAPE TOP                                            
  END-IF                                                  
  WRITE 'TEST:' #WORDS (#C2) (AL=78)                              
  SEPARATE #WORDS (#C2) INTO #PARTS (*) WITH DELIMITER ':'
  IF  #PARTS (1) IS (N3) AND #PARTS (2) IS (N3) 
  AND #PARTS (3) IS (N3) AND #PARTS (4) IS (N3)
    IGNORE                                     
  ELSE                                         
    ESCAPE TOP                                 
  END-IF                                       
  WRITE 'PASS:' #WORDS (#C2) (AL=78)                   
END-FOR                                        
END                                            

Hi Todd,

Thank you very much, It worked, and sure, if I need to adapt it, this is an excellent start.

Thank you again.

And now for IPv6 :lol:

well, it will also detect 1111.2222.3333.4444 as an ip address.

Better do this:

$answer = StringRegExp($MyString1,’((?:\d{1,3}.){3}\d{1,3})’,3)

The sample I posted would not detect the value provided and, while regex is great, this is a Natural Code Samples forum and, to the best of my knowledge, Natural does not have native regex support (at least on Unix, Mainframe, or OpenVMS). If Natural does in fact support regex, please share as I would love to have it in my Natural toolbox.

Thanks.

How can I get the IP NUMBER under NATURAL AJAX (NATURAL ONE) ?
The code to obtain is not so dificult. But what to do with the code ? Where to “compile” within NATURAL AJAX environment ?

Att.
Dayse Vianna