title: Regex Fun
tags: regex, rant, python, pyscripts, scripting, programming, xcode, nsnotificationcenter, macros, refactoring, code
comments: true
date: 2014-05-26 22:03:58+00:00

Today, I went on a regex mission to refactor our code base, and ended up replacing a huge lot of code with macros, which saved us around 450 lines of code throughout the project. Now, we can use those macros to work faster, too!

Here are all the commands I wrote, good luck figuring anything out :p

```bash
$ search_grep NSNotificationCenter\s+
$ search_grep NSNotificationCenter\\s+
$ search_grep "NSNotificationCenter\\s+"
$
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$ search_grep "NSNotificationCenter\\s+"
$
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+selector"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+selector\\((\\w+)\\)"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+@selector\\((\\w+)\\)"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+\@selector\\((\\w+)\\)"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+\\@selector\\((\\w+)\\)"
$ search_grep "\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+selector:@selector\\((\\w+)\\)"
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:(\\w+)\\s+selector:@selector\\((\\w+)\\)\\s+name:(\\w+)\\s+object:(\\w+)\\]"
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$
$ blog_new_post regex-disaster
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:(.*?)\\]"
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*(.*?)\\]"
[[NSNotificationCenter defaultCenter] addObserver:tar selector:@selector(sel) name:nam object:nil]
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:nil]
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*(.*?)\\s*\\]"
[[NSNotificationCenter defaultCenter] addObserver:tar selector:@selector(sel) name:nam object:nil]
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:nil]
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]"
[[NSNotificationCenter defaultCenter] addObserver:tar selector:@selector(sel) name:nam object:nil]
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:nil]
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]"
[[NSNotificationCenter defaultCenter] addObserver:tar selector:@selector(sel) name:nam object:nil]
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:nil]
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]" "NC_ADD\\(\\g<1>, \\g<2>, \\g<3>\\)"
$
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]" "NC_ADD\\(\\g<1>, \\g<2>, \\g<3>\\)" ""
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]" "NC_ADD\\(\\g<1>, \\g<2>, \\g<3>\\)" 0
[[NSNotificationCenter defaultCenter] addObserver:tar selector:@selector(sel) name:nam object:nil]
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:nil]
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]" "NC_ADD\(\\g<1>, \\g<2>, \\g<3>\)" 0
[[NSNotificationCenter defaultCenter] addObserver:tar selector:@selector(sel) name:nam object:nil]
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:nil]
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*nil\\s*\\]" "NC_ADD(\\g<1>, \\g<2>, \\g<3>)" 0
$
$ search_grep "NC_ADD\\(.*?\\)"
$ search_grep "NC_ADD\\(.*?\\);"
$ search_grep "NC_ADD\\(.*?\\);\\s+NC_ADD"
$ search_grep "(NC_ADD\\(.*?\\);)(\\s*?\n)+(\\s*NC_ADD)"
$ search_replace "(NC_ADD\\(.*?\\);)(\\s*?\n)+(\\s*NC_ADD)" "\\g<1>\n\\g<3>"
$ y
$
$ search_grep "(NC_ADD\\(.*?\\);)(\\s*?\n)+(\\s*NC_ADD)"
$ search_grep "(NC_ADD\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)"
$ search_g "(NC_ADD\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)"
$ search_replace "(NC_ADD\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)" "\\g<1>\n\\g<3>"
$
$ search_grep "\\[NSNotificationCenter"
[[NSNotificationCenter defaultCenter] addObserver:tar selector:sel name:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotification:notif]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotification:notif]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
[[NSNotificationCenter defaultCenter] removeObserver:tar]
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s+addObserver:\\s*(.*?)\\s+selector\\s*:\\s*@selector\\s*\\((.*?)\\)\\s+name:(.*?)\\s+object:\\s*(.*?)\\s*\\]" "NC_ADDO(\\g<1>, \\g<2>, \\g<3>, \\g<4>)" 0
$ search_replace "(NC_ADD\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)" "\\g<1>\n\\g<3>"
$
$ search_replace "(NC_ADD\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)" "\\g<1>\n\\g<3>" 0
$ search_replace "(NC_ADDO?\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)" "\\g<1>\n\\g<3>" 0
$ search_replace "(NC_ADDO?\\(.*?\\);)(\\s*?\n){2,}(\\s*NC_ADD)" "\\g<1>\n\\g<3>" 0
$
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotification\\s*:\\s*(.*?)\\s*(.*?)\\s*\\]" "NC_POSTN(\\g<1>)" 0
[[NSNotificationCenter defaultCenter] postNotification:notif]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s*(.*?)\\s+object\\s*:\\s*nil\\s*\\]" "NC_POST(\\g<1>)" 0
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s+object\\s*:\\s*nil\\s*\\]" "NC_POST(\\g<1>)" 0
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s*\\]" "NC_POSTO(\\g<1>, \\g<2>)" 0
[[NSNotificationCenter defaultCenter] postNotification:notif]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
$
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s*\\]" "NC_POSTO(\\g<1>, \\g<2>)" 0
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s*\\]"
[[NSNotificationCenter defaultCenter] postNotification:notif]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
$ search_grep "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s+userInfo\\s*:\\s*(.*?)\\s*\\]"
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*postNotificationName\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s+userInfo\\s*:\\s*(.*?)\\s*\\]" "NC_POSTOU(\\g<1>, \\g<2>, (\\g<3>))" 0
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
$
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*removeObserver:\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s+userInfo\\s*:\\s*(.*?)\\s*\\]" "NC_REMN(\\g<1>, \\g<2>)" 0
$
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*removeObserver\\s*:\\s*(.*?)\\s+name\\s*:\\s*(.*?)\\s+object\\s*:\\s*nil\\s*\\]" "NC_REMN(\\g<1>, \\g<2>)" 0
$
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*removeObserver\\s*:\\s*(.*?)\\s+name\\s*:\\s*(.*?)\\s+object\\s*:\\s*(.*?)\\s*\\]" "NC_REMNO(\\g<1>, \\g<2>, \\g<3>)" 0
[[NSNotificationCenter defaultCenter] removeObserver:tar name:nam object:obj]
$
$ search_replace "\\[\\[NSNotificationCenter\\s+defaultCenter\\]\\s*removeObserver\\s*:\\s*(.*?)\\s*\\]" "NC_REM(\\g<1>)" 0
[[NSNotificationCenter defaultCenter] removeObserver:tar name:nam object:obj]
$
$ search_grep "NC_REM\w*\\(.*?\\)"
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
[[NSNotificationCenter defaultCenter] postNotificationName:nam object:obj userInfo:uInfo]
[[NSNotificationCenter defaultCenter] removeObserver:tar name:nam object:obj]
[[NSNotificationCenter defaultCenter] removeObserver:tar name:nam object:obj]
[[NSNotificationCenter defaultCenter] removeObserver:tar name:nam object:obj]
$

```
