[英]Substitute a part of string in python using regular expressions
我想要的是:
原始字符串: (#1 AND #12) OR #10
轉換為: (something AND another_something) OR something_another
意思是按照#number
替換為唯一字符串
我所做的是:
filter_string = "(#1 AND #12) OR #10"
for fltr in filters_array:
index = fltr[0] #numbers coming from here
replace_by = fltr[1] #this string will replace original one
filter_string = re.sub(r'#'+str(index),replace_by,filter_string)
輸出:
(something AND something2) OR something0
問題:而不是替換#1,而是替換#12和#11,因為#12也具有#1。
我在re.sub()
函數中嘗試使用count = 1
,但由於我的字符串也可以是' (#12 AND #1)
',因此它沒有用。
使用單詞邊界\\\\b
錨來強制數字精確匹配:
filter_string = "(#1 AND #12) OR #10"
filters_array = [(1,"something"),(10,"something_another"),(12,"another_something")]
for num,s in filters_array:
filter_string = re.sub(r'#'+ str(num) +'\\b', s, filter_string)
print(filter_string)
輸出:
(something AND another_something) OR something_another
您可以將元組列表轉換成字典,並使用帶有模式的re.sub
來捕獲數字部分,然后在替換參數中使用lambda表達式按鍵查找正確的值:
import re
filter_string = "(#1 AND #12) OR #10"
filters_array = [(1,"something"),(10,"something_another"),(12,"another_something")]
dt = dict(filters_array)
filter_string = re.sub(r'#([0-9]+)', lambda x: dt[int(x.group(1))] if int(x.group(1)) in dt else x.group(), filter_string)
print(filter_string)
# => (something AND another_something) OR something_another
#([0-9]+)
模式與#
匹配,然后匹配並捕獲到組1中一個或多個數字。 然后,在lambda內部,使用數值來獲取現有值。 如果它不存在, #
+數量將插回結果。
參見Python演示 。
如果需要進一步處理匹配項,則可能要在替換參數中使用回調方法而不是lamda:
import re
filters_array = [(1,"something"),(10,"something_another"),(12,"another_something")]
dt = dict(filters_array)
def repl(m):
return dt[int(m.group(1))] if int(m.group(1)) in dt else m.group()
filter_string = re.sub(r'#([0-9]+)', repl, "(#1 AND #12) OR #10")
print(filter_string)
參見另一個Python演示 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.