Difference between revisions of "Programming:Regular Expressions"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Added recipe for cleaning up copied/pasted directives from nginx wiki/documentation pages.)
m (Broke several entries from the main table into their own sections so they could be explained better.)
Line 10: Line 10:
 
== Notepad++ ==
 
== Notepad++ ==
  
''I'm thinking of breaking the entries from the main table into separate sections to emphasize certain points.''
+
I've listed these for my use with Notepad++, but some may work well for other purposes also.
  
 
=== Match scgi directives in nginx source code ===
 
=== Match scgi directives in nginx source code ===
Line 37: Line 37:
 
|<code>^(.*)</code>
 
|<code>^(.*)</code>
 
|<syntaxhighlight lang="text" enclose="none">            '\1',</syntaxhighlight>
 
|<syntaxhighlight lang="text" enclose="none">            '\1',</syntaxhighlight>
 +
|}
 +
 +
 +
=== Append a pipe character in front of every line ===
 +
 +
This snippet can be used to apply a pipe character in front of every line. You might do this if you are going to cut/paste the content into a MediaWiki table for example.
 +
 +
{| class="wikitable"
 +
|-
 +
!Find what:
 +
!Replace with:
 +
|-
 +
|<code>^(\w)</code>
 +
|<code><nowiki>|\1</nowiki></code>
 +
|}
 +
 +
 +
=== Match characters not used in nginx directive names ===
 +
 +
For example:
 +
 +
<syntaxhighlight lang="text">
 +
2.21 keepalive_disable
 +
</syntaxhighlight>
 +
 +
In this case, we want to match <code>2.21</code> and any spaces before/after the <code>keepalive_disable</code> directive. We could search for that and replace with nothing in order to cleanup the text.
 +
 +
{| class="wikitable"
 +
|-
 +
!Find what:
 +
!Replace with:
 +
|-
 +
|<code>[^a-zA-Z_-]</code>
 +
|
 
|}
 
|}
  
  
 
=== Other ===
 
=== Other ===
 
I've listed these for my use with Notepad++, but some may work well for other purposes also.
 
  
 
{| class="wikitable sortable"
 
{| class="wikitable sortable"
|+Notepad++ Regular Expressions
 
 
|-
 
|-
 
!Regular Expression
 
!Regular Expression
Line 54: Line 85:
 
|Match on C++ (or C99+) comments and all text after
 
|Match on C++ (or C99+) comments and all text after
 
|Selecting comments for removal
 
|Selecting comments for removal
|-
 
|<code>^(\w)</code>
 
|Match the first word character
 
|Notepad++ Search box
 
|-
 
|<code><nowiki>|\1</nowiki></code>
 
|Append a "pipe" in front of the first match found in an earlier search
 
|Notepad++ Replace box
 
 
|-
 
|-
 
|<code>^\s+$</code>
 
|<code>^\s+$</code>
Line 74: Line 97:
 
|Match <em>leading</em> whitespace
 
|Match <em>leading</em> whitespace
 
|Notepad++ Search box; forced text alignment
 
|Notepad++ Search box; forced text alignment
|-
 
|<code>[^a-zA-Z_-]</code>
 
|Match text prior to nginx directives (ex: <code>2.21 keepalive_disable</code>)
 
|Notepad++ Search box; search & replace for code cleanup.
 
 
|}
 
|}

Revision as of 12:08, 26 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.

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


Indent each line 12 spaces, enclose in single quotes and add trailing comma

This snippet can be used to apply appropriate spacing and quoting to lines of text that will be used in a PHP array; specifically directives in a GeSHi language file.

Find what: Replace with:
^(.*) '\1',


Append a pipe character in front of every line

This snippet can be used to apply a pipe character in front of every line. You might do this if you are going to cut/paste the content into a MediaWiki table for example.

Find what: Replace with:
^(\w) |\1


Match characters not used in nginx directive names

For example:

2.21 keepalive_disable

In this case, we want to match 2.21 and any spaces before/after the keepalive_disable directive. We could search for that and replace with nothing in order to cleanup the text.

Find what: Replace with:
[^a-zA-Z_-]


Other

Regular Expression Purpose Useful for
//\s*.*$ Match on C++ (or C99+) comments and all text after Selecting comments for removal
^\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