About Me

My photo
Game Developer, Composer & Gamer.
All my tutorials are categorised into 3 different sections, "The Lounge" for beginner, "Rec Zone" for intermediate and "Pro Series" for advanced tutorials.

Wednesday, September 15, 2010

Rec Zone #1: Using Regex to insert text at start of lines (Notepad++)

Alright i thought i would post this here for my reference as much as yours, because this sort of thing is not easy to search for...

This is straight forward but it's a fairly complex regex operation and only some of you would find this useful.

Ok now today i created a massive list of files, approx 11,000 lines...

There are some files i don't want to be included in the batch, but they are difficult to spot with my eye and i really didn't want to manually search them all 1 by 1 so here is what i did;

I will only use 3 lines as an example...

converter.exe image.png
converter.exe image.jpg
converter.exe image.zip

Now say i wanted to comment out the line that has ".zip" on it, for those 3 lines thats easy, just a matter of putting REM infront of the 3rd line, but when you have tens of thousands of lines, this can get ultra time consuming and you will probably miss a few of them aswell...

Here is how to achieve this using regex and a fairly simple command

In Notepad++, with the file open, press Ctrl + H

Find what: ^(.*zip")
Replace with: REM \1

Now, that should add REM (and a space) to the start of any line that has the text "zip"" in it, also note the quotation mark in the actual "Find what" line, that is important...
When i am creating huge batch files for converting things or extracting or whatever, even if the path has no spaces, i like to put "" quote marks around the pathname, that's why i have the quote in the "Find what" line.
If you don't have quotes around your filenames, then remove the quotemark
BUT BE WARNED:
If you do remove that single quote, the regex expression will now find all text with the letters "zip" in them,
e.g zippy_the_dog.jpg will be found by regex, which is an undesired result.

That is the main reason i add quotation marks around the path/filename

Now i'll explain the command a bit more;

^ means "start of line"

( ) is the logical grouping of part of the expression

. is kinda hard to explain, but it works like the "?" wildcard that some other programs support (single character wildcard NOT like * which is usually a multi-character wildcard)

* as above, * works like a multi-character  wildcard

/1 in Notepad++ regex is the INPUT (FIND WHAT) result... i searched for a bloody long time to find this...

So to break down that command it's;

Find what: ^(.*zip")
Replace with: REM \1

Find what: start of line(any character before and including zip")

Replace with: REM the result of above

So for those 3 lines;

converter.exe image.png
converter.exe image.jpg
converter.exe image.zip

using the regex commands would insert "REM " before every line with zip" at the end

converter.exe image.png
converter.exe image.jpg
REM converter.exe image.zip

Well i hope that's helped someone out there achieve something useful :)

Til the next tutorial,

Jenkins

No comments:

Post a Comment