Programming:Regular Expressions

From WhyAskWhy.org Wiki
Revision as of 18:23, 25 August 2012 by Deoren (talk | contribs) (Fixed display of the replacement text by using the <nowiki> tag)

Jump to: navigation, search


Historically, I've avoided Regular Expressions as I've found them cryptic and very difficult to work with. Over the past few years I've had more need for them and have been using them often enough that I'm getting used to the idea of using them.

Lots of power, but still just as cryptic as ever. This page will list various tips/tricks/recipes that I've found useful.


Notepad++

I've listed these for my use with Notepad++, but some may work well for other purposes also.

Notepad++ Regular Expressions
Regular Expression Purpose Useful for
//\s*.*$ Match on C++ (or C99+) comments and all text after Selecting comments for removal
^(\w) Match the first word character Notepad++ Search box
|\1 Append a "pipe" in front of the first match found in an earlier search Notepad++ Replace box
^\s+$ Match lines that start with whitespace and end with whitespace Notepad++ Search box; search & replace for code cleanup.
\s+$ Match trailing whitespace Notepad++ Search box; search & replace for code cleanup.
^\s+ Match leading whitespace Notepad++ Search box; forced text alignment
[^a-zA-Z_-] Match text prior to nginx directives (ex: 2.21 keepalive_disable) Notepad++ Search box; search & replace for code cleanup.