I have an SQL file loaded up in Sublime Text 2 that has hundreds of strings I need to find and replace. I've been doing them one by one because I couldn't find a regex example and I don't know a lot about regex.
In my file, there are URL's similar to:
https://cdn.mydomain.com/wp-content/uploads/2020/05/20125258/image1.jpg
https://cdn.mydomain.com/wp-content/uploads/2021/10/13440323/image-ex2.jpg
https://cdn.mydomain.com/wp-content/uploads/2012/01/92383422/my-image3.jpg
and so on...
Is there a way to find and select all of the 8 number sequences located before the image file names so I can delete them?
CodePudding user response:
Never fails that as soon as I post a question I find the answer. For anyone else looking for something similar:
The first bracket matches all slashes [/]
The parenthesis capture the group, in this case the group of numbers ([0-9])
The [0-9]
searches the range of numbers between 0 and 9
{8}
is the quantifier, it's there so we look for a group of 8 numbers
That said, My expression I needed was [/]([0-9]){8}[/]
which will select all groups of 8 numbers between slashes like:
https://cdn.mydomain.com/wp-content/uploads/2020/05**/20125258/**image1.jpg
https://cdn.mydomain.com/wp-content/uploads/2021/10**/13440323/**image-ex2.jpg
https://cdn.mydomain.com/wp-content/uploads/2012/01**/92383422/**my-image3.jpg
For what it's worth, this site helped me a lot with writing this and testing it https://regexr.com/
CodePudding user response:
Use
/\d{8}/
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
\d{8} digits (0-9) (8 times)
--------------------------------------------------------------------------------
/ '/'
If you would like to exclude slashes:
(?<=/)\d{8}(?=/)
See another regex proof.
EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\d{8} digits (0-9) (8 times)
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
) end of look-ahead