Please be aware that the content in SAP SQL Anywhere Forum will be migrated to the SAP Community in June and this forum will be retired.

what is the best way to validate a string against IP Address?

I tried using this, it works.

select 'is valid ip address' where '192.168.0.11' similar to '[0-9]+.[0-9]+.[0-9]+.[0-9]+'

But I have problem with this

select 'is valid ip address' where '192.16845444.0.11' similar to '[0-9]+.[0-9]+.[0-9]+.[0-9]+'

asked 08 May '23, 07:03

Baron's gravatar image

Baron
2.1k136149177
accept rate: 48%


Here's an REGEXP sample for IP addresses from the docs – I have not tested it myself:

((2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])\.){3}(2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])

You may also check general regex samples for IP addresses like these, just make sure the syntax is also supported by SQL Anywhere.

permanent link

answered 08 May '23, 08:18

Volker%20Barth's gravatar image

Volker Barth
40.2k361550822
accept rate: 34%

edited 08 May '23, 08:24

thanks for the answer, actually it is not so simple having that the range should be between 0-255, so my approach above is not yet correct.

(08 May '23, 11:38) Baron

Oh, I found it:

select 'is valid ip address' where '192.168.0.1' similar to '[0-9]?[0-9]?[0-9]?.[0-9]?[0-9]?[0-9]?.[0-9]?[0-9]?[0-9]?.[0-9]?[0-9]?[0-9]?'

but is there another simpler one?

permanent link

answered 08 May '23, 07:08

Baron's gravatar image

Baron
2.1k136149177
accept rate: 48%

Is it also possible to change the above statement so that to reject numbers starting with 0?

For example, this should not be validated:

select 'is valid ip address' where '192.168.0.001' similar to '[0-9]?[0-9]?[0-9]?.[0-9]?[0-9]?[0-9]?.[0-9]?[0-9]?[0-9]?.[0-9]?[0-9]?[0-9]?'

(08 May '23, 07:17) Baron
Replies hidden
2

This one should do the trick:

'((2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9]).){3}(2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])'

(09 May '23, 02:52) Frank Vestjens
1

Just to clarify: This is the same expression as the doc sample from my answer with the subtle distinction that is is meant for SIMILAR TO and there does not need to mask the dot whereas the expression from the sample is meant for REGEXP and therefore needs to mask the dot via '\.' to prevent it from being treated as a meta character...

(09 May '23, 04:39) Volker Barth
1

Correct. I referred to the example given by Baron.

By the way in the SQL Anywhere document portal the example for IP addresses in the Regular Expressions Examples is empty.

See https://help.sap.com/docs/SAP_SQL_Anywhere/93079d4ba8e44920ae63ffb4def91f5b/81729adf6ce210149807828f49feb054.html

(09 May '23, 07:58) Frank Vestjens
1

I left a comment on that doc page mentioning the empty text.

(09 May '23, 09:01) JBSchueler
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×22

question asked: 08 May '23, 07:03

question was seen: 367 times

last updated: 09 May '23, 09:01