[2012.4.26-updated]some regex to help you Converting objc code to cpp code

[2012.4.26-updated]some regex to help you Converting objc code to cpp code
0.0 0


Here are some regex script use in vim to help you translate objc code to cpp version.It is not perfect since it just could translate nearly 80% code correctly.
how to use it :
1.open mac terminal
2.run vim open the objec-code file
3.copy the regex below
4.click the edit->paste on the terminal menu
5.prees enter key
6.repeat step 4 - 5 seviral times

last ,if you have a more useful method to translate objc code to cpp code,please comment below,then I will recevied it in my email-box

add nil -> NULL and Happy Christmas!
Improve accuracy.
Add a line regex to Improve accuracy.

:%s/\v–*(\u\u?.{-}) ([^:]
):([^:]{-}) [^:]:([^:]{-}) [^:]:([^:]{-}) [^:]:([^:]{-}) [^:]:([^:][^=])=–/\1::\2(\3,\4,\5,\6,\7)
:%s/\v–*(.{-}) ([^:]
):([^:]{-}) [^:]:([^:]{-}) [^:]:([^:]{-}) [^:]:([^:]{-}) [^:]:([^:][^=])=–/\1->\2(\3,\4,\5,\6,\7)
:%s/\v–*(\u\u?.{-}) ([^:]
):([^:]{-}) [^:]:([^:]{-}) [^:]:([^:]{-}) [^:]:([^:][^=])=–/\1::\2(\3,\4,\5,\6)/g
:%s/\v–*(.{-}) ([^:]):([^:]{-}) [^:]:([^:]{-}) [^:]:([^:]{-}) [^:]:([^:][^=])=–/\1->\2(\3,\4,\5,\6)/g
:%s/\v–*(\u\u?.{-}) ([^:]
):([^:]{-}) [^:]:([^:]{-}) [^:]:([^:][^=])=–/\1::\2(\3,\4,\5)/g
:%s/\v–*(.{-}) ([^:]
):([^:]{-}) [^:]:([^:]{-}) [^:]:([^:][^=])=–/\1->\2(\3,\4,\5)/g
:%s/\v–*(\u\u?.{-}) ([^:]
):([^:]{-}) [^:]:([^:][^=])=–/\1::\2(\3,\4)/g
:%s/\v–*(.{-}) ([^:]):([^:]{-}) [^:]:([^:][^=])=–/\1->\2(\3,\4)/g
:%s/\v–*(\u\u?.{-}) ([^:]
:%s/\v–*(.{-}) ([^:]
:%s/\v–*(\u\u?.{-})) ([^=:]{-})=–/\1)->\2()
:%s/\v–*(\u\u?.{-}) ([^=:]{-})=–/\1::\2()
:%s/\v–*(.{-}) ([^=]


Amazing… Awesome work!
Some suggestions:
# and import > include
# remove brackets from function’s return value, like
> bool, (void) -> void
# If we can add class name from @implementation ClassName to each function like ClaaName::foo would be greate, but seems unachievable via regex.


1.Thank you for reply Wang. my regex has some bug and I’m looking forward to some one wil give a more better one.
2.All your suggestions could be unachievable via regex but a bit tedious, since it’s just regex , if you know how to use regex in vim edit,you can achieve any you want by using regex step by step.
3.All my regex I think is not a finisher but a helper. It is suitable for local change and you know what are doing about.


I added this thread into [[FAQ]]


Text replacement can be done with Notepad++, which is platform independent.


hi,anyone knows how to do it on windows ?


hi jeet mg,
you can use grim for windows in windows.


nice job!


I started a bash script using sed that reads files in a directory and performs the each find/replace on each file in the directory.


Hi steve,
Thx for offering such a well written script.
I just test your script , but it not works very well.
today i update my regex and then you can integrate into your script.
btw, Since my regex can only deal with single line code per times in vim.


I’ve updated the script, and added a small 2nd script.

  • remember to `chmod +x` each script
  • scripts were tested using OSX terminal

objc2cpp.sh - does a find/replace on a single file passed in as argument
objc2all.sh - loops through the files in the current directory, then passes each to the objc2cpp.sh script

Usage: objc2cpp.sh [filename]

objc2all.sh - is a wrapper to call objc2cpp.sh for each file in the current directory

NOTE: If it doesn’t work for you still, let me know what OS and terminal / shell you are running. Could look at converting to Python to help make it more cross-platform. And I will continue to update it as I find better ways to translate code.