Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

RegEx IP validation

former_member194669
Active Contributor
0 Likes
1,699

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?

1 ACCEPTED SOLUTION
Read only

former_member194669
Active Contributor
0 Likes
1,274

I have tried with DEMO_REGEX_TOY but it is giving "invalid regex expression"

Any info?

6 REPLIES 6
Read only

b_deterd2
Active Contributor
0 Likes
1,274

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

Read only

former_member194669
Active Contributor
0 Likes
1,275

I have tried with DEMO_REGEX_TOY but it is giving "invalid regex expression"

Any info?

Read only

0 Likes
1,274

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

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
1,274

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

Read only

former_member194669
Active Contributor
0 Likes
1,274

Ramos,

Thankyou. For pointing out the 120 length limit in REGEX TOY. and the useful link you provided.

Now its working.

Read only

former_member194669
Active Contributor
0 Likes
1,274

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