Regex Find and Replace in Komodo Edit

If you don’t like something change it; if you can’t change it, change the way you think about it. -Mary Engelbreit


This applies to my most commonly used IDE: Komodo.
Lets assume that I want to find and replace (using the regex option in Komodo’s Find and Replace feature) in the following text:

1343217942.jpg
1343217932.jpg
1343217963.jpg
1343218095.jpg

I want to append a _tn to these sample file names. So ’1343217942.jpg’ should turn into ’1343217942_tn.jpg’. To do this I need to use parentheses to make a capturing group out of the part which needs to be retained (the digits part) and then back-reference that group to re-use in the replace. \d will get us digits and surrounding it in (braces) will set it as a capturing group that we can use again. The asterisk * will act as our quantifier so that we can get all the digits.

Find what: (\d*)\.jpg
Replace with: \1_tn.jpg

From this example \1 will back-reference the first capturing group(in the first set of parentheses) that we want to target and retain. If we want to target another capturing group to back-reference, then we’d need to define that in another set of parentheses. Lets change the above text to include some gif files too

1343217942.jpg
1343217932.gif
1343217963.gif
1343218095.jpg

Find what: (\d*)\.(jpg|gif)
Replace with: \1_tn.\2

This should rename all the file names as desired. \2 will back-reference the second capturing group (defined in the second set of parentheses) A cool thing about doing this in Komodo Edit is, you can perform a regex find and replace in a selected chunk of text as well.

 

Categories: How To, Wordpress | 1 comment

One Comment

  1. Cool article Walmik! It’s been awhile since I’ve seen a new article. :)

    Ya know, I really need to get better at regular expressions.

Leave a Reply

Required fields are marked *

*