2008 Jun 24 6:46 PM
Hi All,
I am using the following RegEx for validation of IP address, it is not validating
{^(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5])
.(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5])$}
Any Info?
a®
2008 Jun 24 8:11 PM
I have tried with DEMO_REGEX_TOY but it is giving "invalid regex expression"
Any info?
a®
2008 Jun 24 7:44 PM
Hi,
try something else like,
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
Hope it helps,
Bert
2008 Jun 24 8:11 PM
I have tried with DEMO_REGEX_TOY but it is giving "invalid regex expression"
Any info?
a®
2008 Jun 24 8:53 PM
Hi ,
You can't use this expression on REGEX Toy because the input field allows only 120 characters, and this expression is bigger than this !
Try to test using this code:
REPORT zregex.
DATA ip TYPE string VALUE '10.10.0.10'.
DATA ip2 TYPE string VALUE '10.10.0.'.
DATA ip3 TYPE string VALUE '999.10.0.10'.
DATA validation TYPE string VALUE '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'.
START-OF-SELECTION.
PERFORM validateip USING ip.
PERFORM validateip USING ip2.
PERFORM validateip USING ip3.
FORM validateip USING ip TYPE string.
FIND REGEX validation IN ip.
IF sy-subrc IS NOT INITIAL.
WRITE:/ 'Ip: ', ip, ' is invalid !'.
ELSE.
WRITE:/ 'Ip: ', ip, ' is Valid !'.
ENDIF.
ENDFORM. " validateIP
Greetings,
Marcelo Ramos
2008 Jun 24 8:35 PM
Hi a®s,
You can see a lot of example here regular-expressions.info.
We can find the follow sample:
"
IP Addresses
- Matching an IP address is another good example of a trade-off between regex complexity and exactness.
d{1,3}.d{1,3}.d{1,3}.d{1,3} will match any IP address just fine, but will also match 999.999.999.999 as if it were a valid IP address. Whether this is a problem depends on the files or data you intend to apply the regex to.
- To restrict all 4 numbers in the IP address to 0..255, you can use this complex beast:
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Analyze this regular expression with RegexBuddy (everything on a single line). The long regex stores each of the 4 numbers of the IP address into a capturing group. You can use these groups to further process the IP number.
- If you don't need access to the individual numbers, you can shorten the regex with a quantifier to:
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) Analyze this regular expression with RegexBuddy. Similarly, you can shorten the quick regex to
(?:d{1,3}.){3}d{1,3} Analyze this regular expression with RegexBuddy
"
Font: http://www.regular-expressions.info/examples.html
Based on this examples:
You can do:
REPORT zregex.
DATA ip TYPE string VALUE '10.10.0.10'. " Correct
DATA ip2 TYPE string VALUE '10.10.0.'. " Incorrect
DATA ip4 TYPE string VALUE '999.10.0.10'. " Incorrect
DATA validation TYPE string VALUE ' (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) '.
START-OF-SELECTION.
"// Test all <IPx>
FIND REGEX validation IN ip.
IF sy-subrc IS NOT INITIAL.
"// Error
ELSE.
"// It's OK
ENDIF.
As I Copy & Past the code from internet, please don't reward for me !
Share ours knowledge with all is the SCN's goal
Best regards.
Marcelo Ramos
2008 Jun 25 12:12 PM
Ramos,
Thankyou. For pointing out the 120 length limit in REGEX TOY. and the useful link you provided.
Now its working.
a®
2008 Jun 25 8:13 PM
And also i can use like the following for IP (no check for alid values (0-255))
/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/ 999.999.999.999
Thanks
a®