LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

regex to check IP

I'm not expert in regex... Is this normal behavior?

It looks really weird to me...

 

TiTou_0-1614077811376.png

 

so from now on I'll use this to check if a string is a valid IP address :

 

TiTou_1-1614077976361.png

 


We have two ears and one mouth so that we can listen twice as much as we speak.

Epictetus

Antoine Chalons

Message 1 of 21
(328 Views)

Your regular expression is wrong. The dot matches any character but the newline. Also, [0-9] matches only one digit.

This is my check vi (your new solution is ok though):

IsIPAddress.png

 

Paolo
-------------------
LV 7.1, 2011, 2017, 2019
Message 2 of 21
(305 Views)

[Edit: I was a little too slow, a full solution was already posted.  I'll leave my post below anyway.  The solution from pincpainter uses a different method for designating a literal period character -- enclosing it in square braces.  It also anchors the match at both beginning and end.  The leading '^' means that the match must start with the very first character in the string and the trailing '$' means that the match must end with the very last character in the string.  Anchoring can be helpful when you want it, but you may not always want it.  Removing those anchoring characters lets you match a legal IP that's found anywhere within the input string.]

 

The pattern should be based on something more like "[0-9]+\."

 

The '+' means to match 1 or more of the preceding character (any character in the range from ASCII for "0" through ASCII for "9" - i.e., any digit character).

 

The "." is normally a special character that Match Patttern treats as a 1 character wild card which matches any single character.  Putting a backslash before the period tells Match Pattern to look for a literal period character.  This is referred to as "escaping".

 

Try shortening your original pattern to "[0-9]." and look at the "matched string" output to see what happened with your posted code.  Then change to just "[0-9]+\." to see the difference.

 

 

-Kevin P

 

P.S.  Match Pattern does not support all legal regex'es but executes faster than the more thorough Match Regular Expression.   I generally stick with Match Pattern unless I need to identify a "capture group" (specific substrings that can be returned as distinct outputs)

Message 3 of 21
(302 Views)

You can make the reg ex so it checks the range of the numbers.

 

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

 

But it's a lot for 4 numbers (and not tested):

 

(([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]))

Message 4 of 21
(286 Views)

I think you're proving the point that regex certainly can do this job but might not be the best tool... considering the level of regex expertise is required to check the ranges.

 

Anyway, thanks all for your answers.


We have two ears and one mouth so that we can listen twice as much as we speak.

Epictetus

Antoine Chalons

0 Kudos
Message 5 of 21
(236 Views)

@TiTou wrote:

I think you're proving the point that regex certainly can do this job but might not be the best tool... considering the level of regex expertise is required to check the ranges.

 

Anyway, thanks all for your answers.


It's all about the expected outcome.

 

To some extend, garbage in == garbage out. The trick is to find a good working ground.

 

If you make it fool proof, you're just breeding smarter fools.

Message 6 of 21
(230 Views)

wiebe@CARYA wrote:

@TiTou wrote:

I think you're proving the point that regex certainly can do this job but might not be the best tool... considering the level of regex expertise is required to check the ranges.

 

Anyway, thanks all for your answers.


It's all about the expected outcome.

 

To some extend, garbage in == garbage out. The trick is to find a good working ground.

 

If you make it fool proof, you're just breeding smarter fools.


But if you continue to breed smarter and smarter fools, eventually they become indistinguishable from the non-foolish, which I believe is a noble cause to strive for.  😉

 

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 7 of 21
(209 Views)

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

ValidIP.png

G# - Award winning reference based OOP for LV, for free! ADDQ VIPM Now on GitHub
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 21
(206 Views)

@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).

Paolo
-------------------
LV 7.1, 2011, 2017, 2019
Message 9 of 21
(199 Views)

@billko wrote:

wiebe@CARYA wrote:

@TiTou wrote:

I think you're proving the point that regex certainly can do this job but might not be the best tool... considering the level of regex expertise is required to check the ranges.

 

Anyway, thanks all for your answers.


It's all about the expected outcome.

 

To some extend, garbage in == garbage out. The trick is to find a good working ground.

 

If you make it fool proof, you're just breeding smarter fools.


But if you continue to breed smarter and smarter fools, eventually they become indistinguishable from the non-foolish, which I believe is a noble cause to strive for.  😉

 


Maybe better fools is a better way to put it.

Message 10 of 21
(192 Views)