Skip to content

Instantly share code, notes, and snippets.

@dhcdht
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhcdht/87a3c1adba3e7342661d to your computer and use it in GitHub Desktop.
Save dhcdht/87a3c1adba3e7342661d to your computer and use it in GitHub Desktop.
批量替换 Objective-C 的方法
# -*- coding: utf-8 -*-
__author__ = "dhcdht"
import os
import re
walk_path = 'path_to_project_root'
search_function = 'customSizeWithFont:constrainedToSize:lineBreakMode:'
re_replace = '[\g<1> customSizeWithFont:\g<2>constrainedToSize:\g<3>]'
def re_pattern_from_function_name(function_name):
search_split = function_name.split(':')
# print search_split
re_pattern = '\[([^\]\\n]+?) '
for split_string in search_split:
if split_string:
re_pattern += split_string
re_pattern += ':'
re_pattern += '(.+?)'
re_pattern += '\]'
# print re_pattern
return re_pattern
# print re_pattern_from_function_name(search_function)
def search_replace(root_path, re_pattern, re_replace):
# print root_path
# print re_pattern
# print re_replace
for root, dirs, files in os.walk(root_path):
for file_path in files:
full_path = os.path.join(root, file_path)
if full_path.endswith('.m'):
# print full_path
fr = open(full_path, 'r')
origin_content = fr.read()
# print origin_content
fr.close()
# re_result = re.findall(re_pattern, origin_content, re.DOTALL)
# if re_result:
# print re_result
new_content = re.sub(re_pattern, re_replace, origin_content, flags=re.DOTALL)
# print new_content
fw = open(full_path, 'w')
fw.write(new_content)
fw.close()
re_pattern = re_pattern_from_function_name(search_function)
# print re_pattern
search_replace(walk_path, re_pattern, re_replace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment