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.
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.
02-23-2021 04:59 AM
I'm not expert in regex... Is this normal behavior?
It looks really weird to me...
so from now on I'll use this to check if a string is a valid IP address :
We have two ears and one mouth so that we can listen twice as much as we speak.
Epictetus
02-23-2021 05:25 AM
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):
02-23-2021 05:33 AM - edited 02-23-2021 05:43 AM
[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)
02-23-2021 05:52 AM
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]))
02-23-2021 08:24 AM
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
02-23-2021 08:50 AM
@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.
02-23-2021 09:46 AM
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. 😉
02-23-2021 09:48 AM
Your solution is great TiTou, but if you want to do it manually this is a solution:
02-23-2021 09:53 AM
@Yamaeda ha scritto:
Your solution is great TiTou, but if you want to do it manually this is a solution:
This would make "10. 0. 0. 122" a valid IP, which is questionable (String To IP would refuse to convert).
02-23-2021 10:21 AM
@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.