我有XML,如文件与像标签:替换反斜杠所有斜杠指定标记中的文件路径
<id>SomeID</id>
<datasource>C:/projects/my_project/my_file.jpg</datasource>
<title>My title can include/and other characters</title>
<abstract></abstract>
我想改变这一切斜线反斜杠,但仅在标签数据源(打开和关闭内标签)。
什么是一般的正则表达式语法来做到这一点? 更新:我终于得到了与蟒蛇第一工作液:
regex_01 = re.compile(".*<datasource>")
regex_02 = re.compile("</datasource>.*")
file_content = ""
for line in source_file.readlines():
if "<datasource>" in line:
start = regex_01.search(line).group()
end = regex_02.search(line).group()
part_to_replace = line.replace(start,"").replace(end,"")
replaced = part_to_replace.replace("/","\\")
file_content = file_content + start + replaced.strip() + end + "\n"
else:
file_content = file_content + line
你可以建议一些更优雅?
你可以使用'(* SKIP)(* FAIL)' - 它是什么语言? (仅适用于Perl,PCRE和Python) – antoni
@antoni我开始在记事本++中进行测试,但我认为它不可能在那里做,所以我热衷于** python **解决方案** – Miro