Difference between revisions of "Programming:Regular Expressions"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Added link to Regular Expression Recipes (free sample chapter))
m (Added books, online tools and other useful links)
Line 98: Line 98:
 
|Notepad++ Search box; forced text alignment
 
|Notepad++ Search box; forced text alignment
 
|}
 
|}
 +
  
 
== Additional information ==
 
== Additional information ==
Line 103: Line 104:
 
* [http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/ AddedBytes.com - Regular Expression Cheat Sheet]
 
* [http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/ AddedBytes.com - Regular Expression Cheat Sheet]
 
* [http://stackoverflow.com/questions/3866034/removing-empty-lines-in-notepad removing empty lines in notepad++]
 
* [http://stackoverflow.com/questions/3866034/removing-empty-lines-in-notepad removing empty lines in notepad++]
 +
* [http://stackoverflow.com/questions/283608/using-regex-to-prefix-and-append-in-notepad Using RegEX To Prefix And Append In Notepad++]
 +
* [http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Regular_Expressions How to use regular expressions in Notepad++ - Tutorial]
 +
 +
 +
=== Online Tools ===
 +
 +
* [http://rubular.com/ Rubular, a Ruby regular expression editor]
 +
* [http://gskinner.com/RegExr/ RegExr]
 +
* [http://regexpal.com/ RegexPal]
 +
* [http://www.regextester.com/ REGex TESTER]
 +
 +
 +
=== Books ===
 +
 +
* [[oreilly:0636920012337|Introducing Regular Expressions]] (highly recommended for those new to Regular Expressions)
 
* [[apress:9781590594414|Regular Expression Recipes - Ch 1 (free sample)]]
 
* [[apress:9781590594414|Regular Expression Recipes - Ch 1 (free sample)]]

Revision as of 07:17, 6 September 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


Additional information


Online Tools


Books