Difference between revisions of "Programming:Regular Expressions"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Fixed display of the replacement text by using the <nowiki> tag)
m (Saving progress)
Line 9: Line 9:
  
 
== Notepad++ ==
 
== Notepad++ ==
 +
 +
''I'm thinking of breaking the entries from the main table into separate sections to emphasize certain points.''
 +
 +
=== Match scgi directives in nginx source code ===
 +
 +
This snippet can be used to match <code>scgi</code> directives in nginx source code in order to get a listing of valid directives for the scgi module.
 +
 +
{| class="wikitable" style="text-align: center;"
 +
|-
 +
!Find what:
 +
!Replace with:
 +
|-
 +
|<code>.*(scgi.*)".*</code>
 +
|\1
 +
|}
 +
 +
=== Other ===
  
 
I've listed these for my use with Notepad++, but some may work well for other purposes also.
 
I've listed these for my use with Notepad++, but some may work well for other purposes also.

Revision as of 21:55, 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'm thinking of breaking the entries from the main table into separate sections to emphasize certain points.

Match scgi directives in nginx source code

This snippet can be used to match scgi directives in nginx source code in order to get a listing of valid directives for the scgi module.

Find what: Replace with:
.*(scgi.*)".* \1

Other

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.