1
我有一個文本文件,它看起來像這樣Python的正則表達式:搜索.txt文件字符串
other stuff
set fmri(custom11) "/home/this/is/a/sample_path/to_some/arbitarily/named_11/file-with-othercharacters/file.txt"
other stuff
我想使用Python來搜索文件
- 所有行與
".txt"
- 替換
"11"
與"12"
上的".txt"
行,但只在文件路徑中,而不在"custom11"
字符串中。 - 我忽略了循環邏輯,只關注re.search和re.sub的使用。
if re.search('.txt', line):
print(re.sub("11", "12", line), end='')
不知何故,.TXT不與re.search找到。如果我使用:
if re.search('xt', line):
我得到大部分包含文本文件中的行,而且其他的東西。我如何正確找到'.txt'
文件行?
另外,測試時,該re.sub
取代11
與12
,但也導致"custom11"
換穿"custom12"
。有沒有辦法改變行中的子字符串?
Thanx!第二部分呢 - 是否可以用re.sub替換11到12的子字符串? – user1838663