I have following style url in the text body in mysql DB
- Type A: https://www.old_example1.com/12345678/myname
- Type B: https://www.old_example2.com/87654321/yourname
I would like to change the url to below format
- Type A: https://www.new_example1.com/myname
- Type B: http://www.new_example2.com/yourname
For the domain part, it is a simple replacement query, so no big deal. But the 8 byte random number part after the domain is kinda tricky.
I’ve tried to count text bytes and use substring, but the ‘old_example1’ and ‘old_example2’ part is not always consistent bytes. Sometimes it is 1~2 bytes longer or shorter. But the occurence of ‘/’ is consistent, so I have tried to find a way to count ‘/’ and replace numbers values btwn 3rd and 4th ‘/’ to ” (empty value), but can’t figure out how to apply such conditions.
2
Answers
As Shadow mentioned in the comments that’s an 8 digit number, not 8-byte number.
If your string has always the following format , you could use substring_index function
I am using
.com/
as a word to get the latest part of the string including the replaced one. I’m guessing.com/
is repeated only once in your string, otherwise this answer will fail.Result
Same thing as above to get the digits and to not include twice
/
Result
Finally
Result
See example
You could use
REGEXP_REPLACE()
.Here’s a crude example:
Outputs:
Here’s a db<>fiddle.