Tuesday, January 15, 2013

Notepad++ Regular Expressions - Find/Replace

It's been a quite a while that I wanted to have a full blog post on Regular expressoins to be used on Notepad++ that could immensly help when it comes to string manipulations; which coudln't be done due to lack of time to pay on it; But here it comes.

Following are some of the tips on primitives of the REs on Notepad++
  • [ ] : The square brackets can be used to match ONE of multiple characters. For instance, [abc] matches any of the characters a, b or c. Hence, b[eo]n will match words like ben and bon, but not been or beon. Ranges can also be used, [a-z] is any lower case character and so on.
  • ^ : The caret can be used inside the square brackets to exclude characters from the match. For instance, hell[^o] means the string ‘hell’ will be ignored if followed by the letter ‘o’. Another example is [^A-Za-z] which will exclude all alphabetic characters. However, if not placed inside a set, ^ can be used to matches the start of a line.
  • $ : This matches the end of a line.
  • . : The period or dot matches any character.
  • \d : Matches any single digit.
  • \w : Matches any single alphanumeric characters or underscore.
  • \s : Matches whitespaces including tabs and line breaks.
  • * : The asterisk or star sign matches 0 or more times. For example, Ba*m matches Bm , Bam , Baam etc.
  • + : The plus sign matches 1 or more times. For example, lo+l matches lol , lool , loool etc.
  • \< : Matches the start of a word. For example, \< directly followed by 'sh' matches 'she' but does not matches 'wish'.
  • \> : Matches the end of a word. For example, sh\> matches ‘wish’ and does not matches ‘she’.
  • ( ) : The round brackets can be used in the Find & Replace function to tag a match. tagged matches can then be used in replace with \1, \2 etc. For example, If you write 123xxxRRR in the search and 123\1HHH in the ‘Replace with’ filed, the result will be: 123xxxHHH.
  • \ : The backslash can be used to escape regex characters. For example to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign will have a special meaning.
Hope these examples to be helpful in real time :
  1. Find: Win([0-9]+) Replace with: Windows\1 Will search for strings like Win2000, Win2003 and changes them to Windows2000, Windows2003
  2. Find: [a-z]+(\d\d)\> Replace with: Windows\1 Will search for all alphanumeric followed by 2 digits only at the end such as Win98 and Win07 and changes them to Windows98, Windows07
  3. Fiind : –USD([0-9]+) Replace wtih :  : USD\1 of Entrace Fee NCAA finals–USD200peradult will produce Entrace Fee NCAA finals : USD200 peradult