Search code examples
regexreplacesublimetext3sublimetext

Using Replace to wrap text that start and end with the same thing with some more text


Sorry, the title sounds confusing. I am using SublimeText 3. I am looking to wrap a list of items that start and end with the same thing using Find > Replace....

Example:

These five items start with Screenshot and end with .png.

Screenshot foo.png
Screenshot bar.png
Screenshot text.png
Screenshot abc.png
Screenshot 123.png

Is there a way I can quickly wrap all of these items with "[[File:" and "]]"?

Example:

[[File:Screenshot foo.png]]
[[File:Screenshot bar.png]]
[[File:Screenshot text.png]]
[[File:Screenshot abc.png]]
[[File:Screenshot 123.png]]

Can it be done with Replace and some regex?


Solution

  • search using this regex:

    (Screenshot.*?[.]png)
    

    replace using this regex substitution:

    [[File: \1]]
    

    Output:

    [[File: Screenshot foo.png]]
    [[File: Screenshot bar.png]]
    [[File: Screenshot text.png]]
    [[File: Screenshot abc.png]]
    [[File: Screenshot 123.png]]