Twitter Updates

Saturday, 26 September 2009

Regular Expressions Backreference (Grouping)

One of my favourite features of regular expressions is backreferencing, I always think it is called grouping though.

Simply place () round brackets around part of the expression your interested in and use \1 backslash one to reference the first group. \2 for the second.

For example to strip every thing but the information you want out of a file belonging to a tv series.
Match: (.*)[-. ](S\d\d)-(EP\d\d).*
Replace with : \1-\2-\3

The notation used in the expression:
. :Any Character
* :0 or more of previous character (.* will match anything)
[] :Match any charater in the set (- only works as a literal if it is first)
\d :Match one Decimal number

http://www.regular-expressions.info/brackets.html

No comments: