# How to find IP address

**URL:** <https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples\
**Created:** [May 6, 2016, 5:06am UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330 "2016-05-06T05:06:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Smith](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_smith/32/39012_2.png) [@John\_Smith](https://techcommunity.softwareag.com/u/John_Smith)\
**Post date:** [May 6, 2016, 5:06am UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/1 "2016-05-06T05:06:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tong\_Wang](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/tong_wang/32/410_2.png) [@Tong\_Wang](https://techcommunity.softwareag.com/u/Tong_Wang)\
**Post date:** [May 6, 2016, 4:55pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/2 "2016-05-06T16:55:30Z")

</div>

try regular expression, it should work for your case

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [May 6, 2016, 7:01pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/3 "2016-05-06T19:01:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_Smith](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_smith/32/39012_2.png) [@John\_Smith](https://techcommunity.softwareag.com/u/John_Smith)\
**Post date:** [May 7, 2016, 2:17am UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/4 "2016-05-07T02:17:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [May 7, 2016, 12:19pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/5 "2016-05-07T12:19:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Todd\_Beckwith](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/todd_beckwith/32/388_2.png) [@Todd\_Beckwith](https://techcommunity.softwareag.com/u/Todd_Beckwith)\
**Post date:** [May 9, 2016, 5:06pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/6 "2016-05-09T17:06:17Z")

</div>

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.

```auto
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                                            
```

---

<div class="post-metadata">

**Author:** ![John\_Smith](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_smith/32/39012_2.png) [@John\_Smith](https://techcommunity.softwareag.com/u/John_Smith)\
**Post date:** [May 10, 2016, 2:13am UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/7 "2016-05-10T02:13:21Z")

</div>

Hi Todd,

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

Thank you again.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [May 10, 2016, 4:29pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/8 "2016-05-10T16:29:02Z")

</div>

And now for IPv6 :lol:

---

<div class="post-metadata">

**Author:** ![David\_Stephan](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@David\_Stephan](https://techcommunity.softwareag.com/u/David_Stephan)\
**Post date:** [November 4, 2016, 11:51am UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/9 "2016-11-04T11:51:42Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Todd\_Beckwith](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/todd_beckwith/32/388_2.png) [@Todd\_Beckwith](https://techcommunity.softwareag.com/u/Todd_Beckwith)\
**Post date:** [November 4, 2016, 12:38pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/10 "2016-11-04T12:38:03Z")

</div>

> [@David Stephan](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Dayse\_Vianna1](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Dayse\_Vianna1](https://techcommunity.softwareag.com/u/Dayse_Vianna1)\
**Post date:** [April 19, 2017, 10:12pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/11 "2017-04-19T22:12:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 3:35pm UTC](https://techcommunity.softwareag.com/t/how-to-find-ip-address/188330/12 "2021-04-02T15:35:59Z")

</div>


