0

I have encountered a problem with regex in SQL Server.

I have a query like this:

SELECT 1 
WHERE '/test/url/1' LIKE '/test/url/%[0-9]'

but there is a problem in this regex.

When I enter a non-valid URL like /test/url/1w1, it returns 1 and regex does not work correctly.

I want to validate an integer in URL with any length as :ID parameter.

Please recommend a correct regex for this situation.

2
  • sql-server weak for regex. Commented Apr 13, 2019 at 7:14
  • SQL SERVER 2017 14.0.1000.169 Commented Apr 13, 2019 at 7:16

1 Answer 1

1

You can use:

SELECT 1
WHERE '/test/url/1' LIKE '/test/url/%[0-9]' AND
      '/test/url/1' NOT LIKE '/test/url/%[^0-9]%'

This requires an integer (0-9) at the beginning of the last path-segment but avoids that the segment ends with an integer.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not a valid SQL expression. Why was it accepted as the answer (and upvoted)?
I am not sure Gordon. David edited this last and My answer was just "NOT LIKE '/test/url/%[^0-9]%'". Maybe the answer was accepted and upvoted before edit? Are you pointing out the missing 'And' condition here??
here is the reference to regex in SQL learn.microsoft.com/en-us/sql/ssms/scripting/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.