Difference between revisions of "Programming:Regular Expressions"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Added another regex to the list)
m (Fixed display of the replacement text by using the <nowiki> tag)
Line 27: Line 27:
 
|Notepad++ Search box
 
|Notepad++ Search box
 
|-
 
|-
|<code>|\1</code>
+
|<code><nowiki>|\1</nowiki></code>
 
|Append a "pipe" in front of the first match found in an earlier search
 
|Append a "pipe" in front of the first match found in an earlier search
 
|Notepad++ Replace box
 
|Notepad++ Replace box

Revision as of 19:23, 25 August 2012


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.