Programming:Regular Expressions

From WhyAskWhy.org Wiki
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.

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_-]


Remove all text other than a MAC Address

Still needs some work

Match MAC Addresses with either - or : as separators using non-capturing parenthesis.

Find what: Replace with:
^.*(\w{2}(?:-|:)\w{2}(?:-|:)\w{2}(?:-|:)\w{2}(?:-|:)\w{2}(?:-|:)\w{2}).* \1


MAC Addresses - Convert dashes to colons

Match MAC Addresses with - and replace with : as separators using capturing parenthesis.

Find what: Replace with:
(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2}) \1:\2:\3:\4:\5:\6


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
(\w{2}(-|:)\w{2}(-|:)\w{2}(-|:)\w{2}(-|:)\w{2}(-|:)\w{2}) Match MAC Addresses with either - or : as separators Notepad++ Search box?
(\w{2}(?:-|:)\w{2}(?:-|:)\w{2}(?:-|:)\w{2}(?:-|:)\w{2}(?:-|:)\w{2}) Same as above, but using non-capturing parenthesis instead Notepad++ Search box?

GNU grep

IP Addresses

grep "\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b" /path/to/file


Additional information


Online Tools


Books