Thanks Buck,
As I use regular expressions quite infrequently I asked ChatGPT to explain
it.
Would you agree with the description ?
'^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}+
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$'
Certainly! This regular expression represents an IP address validation
pattern. It checks whether a given string represents a valid IPv4 address.
Let's break it down step-by-step:
^ - The caret symbol at the beginning means the expression must match from
the start of the string.
(?: ... ) - This is a non-capturing group. It groups multiple patterns
together but doesn't store the matched result. It is used here to group
the IP address segments.
25[0-5] - Matches any number from 250 to 255.
| - Acts as an OR operator, allowing alternatives.
2[0-4][0-9] - Matches any number from 200 to 249.
| - OR. 1[0-9][0-9] - Matches any number from 100 to 199.
| - OR. [1-9][0-9] - Matches any number from 10 to 99.
| - OR. [0-9] - Matches any single digit number from 0 to 9.
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) - This part
represents one of the four segments of the IP address (e.g., 192, 168,
etc.).
\. - Escaped dot. It matches a literal dot (.), used to separate IP
address segments.
{3} - The curly braces with the number 3 mean that the previous group (the
IP address segment and the dot) should repeat exactly 3 times. So, it will
match three segments and two dots.
+ - The plus sign after {3} indicates that the preceding group (segment
and dot) should repeat one or more times. This allows it to match the last
segment of the IP address.
The second part:
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) - Similar to the
first part, it represents the last segment of the IP address (e.g., 1,
255, etc.).
$ - The dollar sign at the end of the expression ensures that the pattern
matches the end of the string. This means the IP address should not have
any characters after the last segment.
In summary, the regular expression checks whether the input string is a
valid IPv4 address in the format "xxx.xxx.xxx.xxx" where each "xxx"
segment is a number between 0 and 255.
Thanks
Don
From: "Buck Calabro" <kc2hiz@xxxxxxxxx>
To: rpg400-l@xxxxxxxxxxxxxxxxxx
Date: 26/07/2023 03:11 AM
Subject: Re: Issue with regular expressions.
Sent by: "RPG400-L" <rpg400-l-bounces@xxxxxxxxxxxxxxxxxx>
On 7/25/2023 12:33 PM, Jason Olson wrote:
When I do this,
exec sql
select
REGEXP_COUNT('192.168.140.230', :ip_regex)
into :count from SYSIBM.SYSDUMMY1;
It's working fine. However, if I use a variable to hold the IP address
it fails. I've tried char fields and varchar fields and it always fails.
This is on 7.3.
Without seeing code or regex, my guess is that there's a fixed length
variable (trailing blanks) being assigned to a varchar. FWIW, this works
for me on 7.4:
dcl-s ip varchar(64) inz('192.168.140.230');
dcl-s ip_regex varchar(128) inz;
dcl-s count int(10) inz;
ip_regex =
'^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}+
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$';
exec sql
select
REGEXP_COUNT('192.168.140.230', :ip_regex)
into :count from SYSIBM.SYSDUMMY1;
dump(a) 'literal';
exec sql
select
REGEXP_COUNT(:ip, :ip_regex)
into :count from SYSIBM.SYSDUMMY1;
dump(a) 'variable';
*inlr = *on;
As an Amazon Associate we earn from qualifying purchases.