The Dopefly Tech Blog
<< The Dopefly Tech Blog Main page
Post A Comment!
Use regular expressions to be clever in your IDE
posted under category: General on December 6, 2007 at 5:44 pm by MrNate
Here's something I do almost daily it seems. So much so that I take it for granted. A couple months ago a friend was looking over my shoulder and he exclaimed "What the--? You've got to save that and send it to me!" I explained how it was just throw-away code, and I could send it to him if he wanted. The code is long gone but that reaction stuck in my mind, so let's dive in with an example.
You have a list of values on lines and you want to want to apply some code to each:
Select the lines, and use your IDE's find/replace tool with the regex option. In Eclipse, this is just CTRL+F and check the checkbox. Use this as the search pattern:
This regex says to select any line with at least one character in it and store it in a character group.
Some code like this would be the replace pattern:
querySetCell(myQuery, "$1", "");
Replace them all and your code will be generated in an instant. The regex will drop each line's content into the $1 backreference.
There are dozens of other types of uses just like this one.
On Dec 6, 2007 at 5:42 PM James Moberg (james at the ever popular ssmedia.com) said:
Sometimes I'm using a weak text editor that doesn't have good search-and-replace features... I've managed to write a bunch of macros using AutoHotKey so that they work anywhere regardless of the program I'm using and can be easily synched with my portable USB stick.
For example, I've got:
- lowercase, uppercase, titlecase, lower quotecase and extended character replacement hotkeys
- a hotkey to clean up nasty CSS
- lost of stored snippets
- program launcher shortcuts
I was using Microsoft Access for a project and had to do some manual clean up and delete individual rows. It was really slowing me down to have to interact with the "Are you sure" dialog box, so I wrote the following script that does it all automatically for me when I press CTRL-SHIFT-X only while using Access:
IfInString, Title, Access
I had another situation where I copied a column of records from Access/Excel and had to turn it into a comma-delimited list. I was pasting data into my text editor, replacing all carriage returns with commas and then deleting the first list item if it was "ID". I rewrote this into a macro using WINDOWSKEY+V:
StringGetPos, pos, clipboard, ID`,
if pos = 0
StringRight, clipboard, clipboard, StrLen(clipboard)-3
Again, AutoHotKey works with any program and is a great time saver.
On Dec 6, 2007 at 7:13 PM Max Hamby (email@example.com who would have preferred an address at jigsawsol.com) said:
Nice one, Nathan! I wish I'd have seen this post 2 days ago... would have saved me a lot of time.
How about sharing more of the "dozens"?
On Dec 6, 2007 at 7:33 PM Justin Carter (justin.w.carter at the endearing gmail.com) said:
Nice tip, thanks! I'm surprised that after all these years this is the first thing I've ever read about doing a regex find/replace in an IDE! :)
On Dec 6, 2007 at 9:17 PM Nathan Strutz (http://www.dopefly.com/) said:
great tip, thanks!
How about I'll post more when I come across them. I remember a few times I've used it to suck out columns from pasted spreadsheet data and CSV files, and to find certain types of method calls with arguments not in a list of preset known arguments, and to remove whitespace, line breaks, etc.
Be creative and share what you find :)
On Dec 6, 2007 at 9:28 PM Max Hamby (mhamby at the ever popular jigsawsol.com) said:
I've used regex to scrub client data (always a nightmare!) and reformat some lists but had never thought to use it for actually producing code.
Hmmmm, got me thinking over here....
On Dec 7, 2007 at 11:02 AM Adam (www.dustypixels.com) said:
Wow, quite amazed more people don't use this as second nature already. I'm no regex pro but just some basic skills can save you tons of typing. The 'Pragmatic Programmer' book describes regex as one of the basic skills every coder should have in their toolbox.
On Dec 7, 2007 at 12:24 PM James Moberg (james, who eats ssmedia.com) said:
If you want to use this same regular expression as a hotkey that can be used in any application (even ones that don't support basic search-and-replace), here's the AutoHotKey equivalent that you can always have handy.
It will take your clipboard and perform the search-and-replace. I have a lot of these shortcuts and it's great being able to use them with any program.
#q:: ; Windows Key + Q
clipboard := RegExReplace(clipboard, "(.+)", "querySetCell(myQuery, ""$1"", """");")
On Dec 7, 2007 at 3:59 PM David Buhler (DavidBuhler, who eats gmail.com) said:
Any thoughts on a similar topic would be greatly appreciated!
On Dec 7, 2007 at 5:09 PM James Moberg (james sends love to ssmedia.com) said:
Post A Comment!
I personally refrain attempting to beautify all CFML code and prefer to have occassional optimized blocks or blocks that need the protected whitespace (like CFMail and text messages).