0

I need to replace Strings like '1956678~26789~7230' and '1956678~26789' in https://baseurl/api/something/1956678~26789~7230/client/1956678~26789 with say like X. so it will be like https://baseurl/api/something/X/client/X. I was using the replace function, but I am not able to find the regex that I can use to replace the desired characters with X

So I want to use patindex to find position of following String in a given string. what is sql server regex version that can match all of the following strings: 1956678~26789~7230, 9889~454~234234~9089, 1~1~234. Eventually, I will use the substring and replace function to replace all of the above string with symbol X.

hhafeez
  • 73
  • 9
  • `b{2,}` will match `b` 2 or more times. See https://regex101.com/r/U6l7bl/1 – MonkeyZeus Aug 26 '19 at 17:16
  • its not working in sql though. I tried select REPLACE ('https://baseurl/api/something/bbbbb/client/bbbbbbbb','b{2,}','handle') api but same string I get back – hhafeez Aug 26 '19 at 17:20
  • 3
    SQL server does not easily support regex: [Regex pattern inside SQL Replace function?](https://stackoverflow.com/questions/21378193/regex-pattern-inside-sql-replace-function). `REPLACE()` can only handle exact string replacements. – MonkeyZeus Aug 26 '19 at 17:23
  • nope! here I have trouble finding regex that can be supplied to the second argument of replace. – hhafeez Aug 26 '19 at 17:24
  • 1
    That's too bad for you because `REPLACE()` does not support regex. – MonkeyZeus Aug 26 '19 at 17:29
  • 1
    Just as a note: SQL Server 2008 and 2008 R2 are **out of extended support** by now - https://www.red-gate.com/simple-talk/sql/database-administration/the-end-of-sql-server-2008-and-2008-r2-extended-support/ - time to upgrade! – marc_s Aug 26 '19 at 18:20
  • this content maybe useful for solve your problem. you can use SqlRegex.dll for do this.https://www.sqlservercentral.com/blogs/regex-in-sql-server-for-replacing-text – Mahdi Rahimi Sep 02 '19 at 11:15
  • okay if i use patindex which take the regex, then what will be my regex for the above patter?I can use later substring with replace to do my task. – hhafeez Sep 03 '19 at 17:09

0 Answers0