利用正規表示式進行修改自定義ini配置檔案

2021-09-09 04:01:47 字數 3333 閱讀 8354

正規表示式比較複雜,前段時間花了一些時間研究,但一直沒能與實際工作結合起來,這幾天修改配置檔案,因牽涉的檔案極其多,逐個修改工作量很大,便考慮寫個工具,以後再改就方便了很多,結果就被正規表示式給難住了,原來符號在正則中是有特別用處的,一時疏忽,結果搞了好久。

解決的問題如下:程式搜尋某個目錄下的配置檔案,找到後將配置檔案的某個session下的列表替換掉。

打算分三步走

1、建立函式replace_session(filename, sessionname, *alist),用以替換指定檔案中,指定sessionname中的內容

2、查詢指定目錄下的指定檔案,得出指定檔案列表

3、將1和2組合起來,實現查詢檔案並替換對應sessionname的功能。

**如下:

mycode

# !usr/bin/env python2.7

# coding: utf-8

# filename: find_replace_ini.py

import findfile

import replaceini

filelist = findfile.find_file_by_pattern("ipad_set.ini")

if(len(filelist) <= 0):

print("not find file.")

for file in filelist:

print(file)

replaceini.replace_session(file, "nowl2_dest_servers",

"192.168.0.1:25", "192.168.0.3:110")

mycode

# !usr/bin/env python2.7

# coding: utf-8

# filename:findfile.py

import

reimport

osdef find_file_by_pattern(pattern='.*', base=".", circle=true):

'''查詢給定資料夾下面所有 '''

re_file = re.compile(pattern)

if base == ".":

base = os.getcwd()

final_file_list =

#print (base)

cur_list = os.listdir(base)

for item in cur_list:

if item == ".svn":

continue

full_path = os.path.join(base, item)

if full_path.endswith(".doc") or \

full_path.endswith(".bmp") or \

full_path.endswith(".wpt") or \

full_path.endswith(".dot"):

continue

# print full_path

bfile = os.path.isfile(item)

ifos.path.isfile(full_path):

if re_file.search(full_path):

else:

final_file_list += find_file_by_pattern(pattern, full_path)

return final_file_list

if __name__ == "__main__":

filelist = find_file_by_pattern("ipad_set.ini")

for file in filelist:

print(file)

mycode

# !usr/bin/env python 2.7

# coding: utf-8

# filename: replaceini.py

# 替換配置檔案中某個session下的字串陣列

import

redef replace_session(filename, sessionname, *alist):

pattern = re.compile(r'[.*]')

datalist0 =

datalist1 =

datalist2 =

start = 0

file_object = open(filename, "r")

list_of_all_the_lines = file_object.readlines()

for line in list_of_all_the_lines:

if(line.find("".format(sessionname)) != -1):

start = 1

elif (re.compile(r'[.*]').search(line)):

if(start == 1):

start = 2

if(start == 0):

if(start == 1):

if(start == 2):

file_object.close()

datalist1_replace = ;

if(len(datalist1) > 0):

for line in alist:

for line in datalist1[1:]:

if(re.compile(r'\s*#').match(line)):

else:

print("not find session, , ".format(filename, sessionname ))

return

file_object = open(filename, "w")

for line in datalist0:

file_object.write (line)

for line in datalist1_replace:

file_object.write (line)

for line in datalist2:

file_object.write (line)

file_object.close()

if __name__ == "__main__":

replace_session("ipad_set.ini", "nowl2_dest_servers",

"192.168.0.1:25222", "192.168.0.3:110")

print("done")

Excel自定義函式使用正規表示式詳解

函式exstr功能,根據正規表示式,替換或判斷或提取字串 引數 str 源字串 引數 parttern 正規表示式字串 引數 actionid 函式功能 1正則方法一,替換符合正規表示式的字元 正則方法二 判斷是否有符合正規表示式 正則方法三,提取符合正規表示式的字元 引數 repstr 指定替換的...

正規表示式定義

正規表示式定義 正規表示式 regular expression 描述了一種字串匹配的模式,可以用來檢查乙個串是否含有某種子串 將匹配的子串做替換或者從某個串中取出符合某個條件的子串等。列目錄時,dir txt或ls txt中的 txt就不是乙個正規表示式,因為這裡 與正則式的 的含義是不同的。正規...

正規表示式 定義

前言 在前端頁面使用中,遇到日期格式的驗證,開始使用了一款表單控制項驗證,但是不相容!而且使用起來還受到很大約束,所以就決定自己寫原生js。為了完成日期格式的驗證,第一步,當然是學會使用正規表示式。以前都是直接複製使用,這次理解原理,自己原生寫。首先,總結了正規表示式的定義。正規表示式 正式定義 乙...