4

My skills are very basic. I'm hoping to find a regular expression for Sublime Text 3 that finds a string by beginning and end. I'd like to keep the middle unchanged and I only want to replace the beginning and the end.

For instance. Search for url with /shop/ at the beginning and .htm at the end. I'd then like to replace /shop/ with /product/ and replace .htm with a forward /

e.g.

http://www.website.com/shop/ford-truck-hitch-extension.htm

becomes

http://www.website.com/product/ford-truck-hitch-extension/

This bit finds the instances but I can't figure out how to replace the beginning and end.

shop[^<]+.htm
Enissay
  • 4,754
  • 3
  • 25
  • 48
Pmaridnah
  • 43
  • 1
  • 4

3 Answers3

3

Find this:

/shop/(.*?)\.htm

And replace with this:

/product/\1/
AMDcze
  • 486
  • 3
  • 13
2

It can be done with a single regex, try:

com\/\Kshop(\/.*?)\.htm

and replace with:

product\1/

DEMO

  NODE                     EXPLANATION
--------------------------------------------------------------------------------
  com                      'com'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  \K                       Resets the starting point of the reported match
                           ie: Everything that has been matched till this 
                               point won't be changed
--------------------------------------------------------------------------------
  shop                     'shop'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \/                     '/'
--------------------------------------------------------------------------------
    .*?                    any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  htm                      'htm'

To read more about regex, feel free to visit The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Enissay
  • 4,754
  • 3
  • 25
  • 48
0

So, you're not actually looking for the beginning of the string. You're looking for part of the string after ".com". As for end of string, $ will match it.

I would also suggest using two expressions, as it makes the two substitutions more obvious.

Depending on how specific you need to be, the pair of expressions:

s|com/shop/|com/product/|
s|\.htm$|/|

will do the pair of substitutions you want. The first will also work on anything that for some reason has the substring "com/shop/" though, so as I said -- if you need to improve the specificity you can.

If you absolutely need to do it in one:

s|com/shop/(.*)\.htm$|com/product/\1/|

Will select and save all the content between the parenthesis and re-insert it into your final result, allowing you to make the two changes you're looking for.

PS: I'm not sure how sublimetext handles escaping -- some of these (such as the parenthesis or backslash) may require escaping

zebediah49
  • 7,177
  • 1
  • 28
  • 48