#!usr/bin/evn python # -*- coding:utf-8 -*- import os,sys,pyperclip import easygui as g import re file_name='target_code' with open(file_name,'r',encoding='utf-8') as rf: old_text=rf.read() replace_code=r'''void test3(void) { int a=10; char s[10]={0}; printf("a=%d,s=%s ",a,s); } ''' bracketsnum=replace_code.count('{') pat=replace_code.split(' ')[0] pat=pat.replace('(','(') pat=pat.replace(')',')') pat=pat.strip('{') # begin coding regex repat='{:s}{}{}{}{}'.format(pat,'.*{.+}','{','{:d}'.format(bracketsnum),'}') patt=re.compile(repat,re.M|re.S) print(patt) result=re.findall(patt,old_text)