Wednesday, 3 January 2018

Handy Regex Notes for Notepad++



Here are some regex expressions I have found to be useful with notepad++ as a programmer:
  1. .*' - (dot star apostrophe), finds all  occurrences of "'s".
  2. .*word - finds all occurrences of any number of characters up to the word "word".
  3. word.* - finds all occurrences of the word "word" followed by any number of characters.
  4. [a-z][a-z][a-z].com  - finds all three letter .com domain names(lower case). Simply change the a-z to uppercase to look for uppercase 3 letter domains.
  5. [a-z][a-z].com - finds all two letter .com domain names in lower case. Change to A-Z for uppercase.
  6. [0-9][0-9][0-9].com - finds all three number .com domain names.
  7. Adult T - [0-9a-zA-Z ]+, - finds all occurrences of "Adult T -" followed by another word or number(or combo) and then a comma.
  8. .*[\|]  - Finds and replace all but the domain name in cases where ea. line in a file looks like: celebrification.com | scottludbrook@aanet.com.au(make sure that the regex ends in a space). Note that the pipe character is escaped with a backslash.
  9. ^\s - find are replace all blank lines. Deletes blank lines when search for this and replace with nothing.

Make all lowercase

While not a regex, I learned how to change all characters to lowercase using Notepad++ today as well. All you do is select the entire document or the part you want lowercase and right click then select "lowercase" and DONE!

Find & Replace Part of a String

Lets say you have a CSV file with these contents:
1 Adult T - Led Zeppelin, black, XL
2 Adult T - Motley Crue, black, Med
3 Adult T - Grateful Dead, tie die, Sm
...and you need to replace:
"Adult T - Grateful Dead," with "Grateful Dead T-Shirt,"
This is where the \1, \2, \3 abilties of Notepad++ version 6+ comes in very handy. First you define parts one and two in parenthesis, so as in the #7 example above you'd change it to this instead:
Search for:
(Adult T - )([0-9a-zA-Z ]+),
and replace with:
\2 T-Shirt,
The above tells Notepad to find the text "Adult T - " and any string after it until you hit a comma. Then in the last line above, it says to replace it with what is in the 2nd parenthesis and to add T-Shirt to it so "Adult T - Led Zeppelin," becomes "Led Zeppelin T-Shirt,". Works like a charm!
By reading and studying the above examples, you should be able to modify the regex expressions to do just about anything. I am using only simple regular expressions here to make it easier to understand because most of us have a terrible time understanding all the regular expression rules.

0 comments:

Post a Comment