From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

regex to check IP

https://regex101.com/ is a great site to test your regexes without having to make a million cases in LabVIEW. It also has an "explanation" panel to the side that gives you some tips on how to use it.

Message 11 of 21
(1,244 Views)

@pincpanter wrote:

@Yamaeda  ha scritto:

Your solution is great TiTou, but if you want to do it manually this is a solution:

ValidIP.png


This would make "10. 0. 0. 122" a valid IP, which is questionable (String To IP would refuse to convert).


IP Test.png

 

Not sure if this is enough. It doesn't accept 0001, maybe it should. It does accept 001, not sure it should...

Message 12 of 21
(1,241 Views)

@BertMcMahan wrote:

https://regex101.com/ is a great site to test your regexes without having to make a million cases in LabVIEW. It also has an "explanation" panel to the side that gives you some tips on how to use it.


Do mean you make mistakes with reg.ex.? 😉

Message 13 of 21
(1,238 Views)

wiebe@CARYA  ha scritto:

@pincpanter wrote:

@Yamaeda  ha scritto:

Your solution is great TiTou, but if you want to do it manually this is a solution:

ValidIP.png


This would make "10. 0. 0. 122" a valid IP, which is questionable (String To IP would refuse to convert).


IP Test.png

 

Not sure if this is enough. It doesn't accept 0001, maybe it should. It does accept 001, not sure it should...


It probably depends on the context.

Just tried with Titou's code: 100.23.45.67 is translated back to the same address, but 0100.023.045.067  is translated back to 64.19.37.55 (as is 00100.0023.0045.0067). That means that any trailing 0 makes the function interpret the number as octal.

[Edit: as clearly stated in the help, of course: but I never noticed this]

 

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
Message 14 of 21
(1,225 Views)

The farther this discussion goes, the more I like the original alternative:

 


@TiTou wrote:

TiTou_1-1614077976361.png

 


-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 15 of 21
(1,203 Views)

wiebe@CARYA wrote:

 

(([012]?[0-9][0-9])|([0-9]?[0-9]))\.(([012]?[0-9][0-9])|([0-9]?[0-9]))\.(([012]?[0-9][0-9])|([0-9]?[0-9]))\.(([012]?[0-9][0-9])|([0-9]?[0-9]))


Matches some none IP V4 numbers like 292.168.1.5

 

I use the PCRE

\A(?:(?:\d|\d\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d|\d\d|1\d\d|2[0-4]\d|25[0-5])\Z

Message 16 of 21
(1,190 Views)

I faintly remember entries in the forum where the IP to String function had side effects on the network stack, freezing VIs.

 

.NET should work fine (https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.parse?view=net-5.0)

LLindenbauer_0-1614105483874.png

 

 

Also, just for completenesses sake: The task to validate an IP address by regex is so common it even has its own website: http://ipregex.com/

Message 17 of 21
(1,181 Views)

wiebe@CARYA wrote:

It doesn't accept 0001, maybe it should. It does accept 001, not sure it should...


Leading zeros are acceped sometimes. Leading zeros are not allowed, they are used for octal notation.

0 Kudos
Message 18 of 21
(1,177 Views)

@Martin_Henz wrote:

wiebe@CARYA wrote:

It doesn't accept 0001, maybe it should. It does accept 001, not sure it should...


Leading zeros are acceped sometimes. Leading zeros are not allowed, they are used for octal notation.


Hm..

 

If the reg.ex. is to support octal numbers, it's going to be even more complex.

 

 

I have used the string to IP trick before.

 

The string also accept "localhost", and IP to string will turn that into 127.0.0.1 or the PC's name (depending on the notation).

 

The string also accepts resolvable PC names, and even URLs. "www.google.com" resolves to 142.250.179.132, for instance.

 

If the goal is to validate a dot notation, it works. Except for octal inputs.

 

If the goal is to validate the string as a valid address, the IP to string output should be tested against 255.255.255.255 and 0.0.0.0.

 

Hard stuff...

0 Kudos
Message 19 of 21
(1,126 Views)

TLDR;

 

Here's how I check for a valid IP address.

 

Check_IP.png

 

LOL, I just noticed someone already posted MY CODE!

 

I will take that as a compliment 😛

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 20 of 21
(1,105 Views)