[英]Python Search and Replace Using a List for Search
我有幾行我正在循環並存儲為字符串的文件,我希望使用 python 字符串 str.replace str.replace()
中內置的方法或使用正則表達式在每一行中執行簡單的搜索和替換re.sub()
但使用列表作為舊 substring 的參數。 我知道格式通常如下:
string.replace('oldsubstring','newsubstring')
但是,如果我有一個字符串列表: ['word1', 'word2', 'word3']
,是否可以將其用作oldsubstring
參數,以便如果在string
中找到列表中的任何元素,那元素被替換為newsubstring
。 我知道這可以使用循環遍歷我的所有行和字符串列表的雙重嵌套 for 循環來實現,但我正在尋找一種更有效的算法來實現這一點。
跟進問題:
我發現的另一個問題是有時我的字符串列表看起來像:
['word1', 'word1_suffix', 'word2', 'word3']
注意:這些元素的順序不保證每次運行都相同。
使用雙嵌套for循環方法時,如果word1_suffix
出現在我正在查看的當前行中,然后我循環遍歷我的字符串列表,如果word1
恰好首先出現在我的字符串列表中,則替換將是newsubstring_suffix
而不是而不是用newsubstring
word1_suffix
注意:我知道使用正則表達式我可以確保word1_suffix
是它自己的由空格包圍的完整單詞,但有時我確實希望我的行的一部分遵循以下格式: word1_miscellaneous
被替換為newsubstring_miscellaneous
以便該方法不會完全解決我的問題。
使用re.sub
您可以使用正則表達式的貪婪字符來確保word1_suffix
不會被newsubstring_suffix
替換:
your_string = "hello word1_suffix world word3"
word_list = ['word1', 'word1_suffix', 'word2', 'word3']
word_set = set(word_list)
# pattern to match all 'words' (succession of letters, digits and _):
word_pattern = re.compile(r'\w+')
print(re.sub(word_pattern, lambda x: "newsubstring" if x.group() in word_set else x.group(), your_string))
lambda function 檢查匹配組是否在word_set
中並將其替換為newsubstring
。
Output:
hello newsubstring world newsubstring
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.