python - 如何在 Python 中使用正则表达式查找和替换 URI 片段?
问题描述
你好呀!
我正在尝试在文本文件中查找和替换 URI 片段,但我只是不知道如何做到这一点。
一些资源以 URL (Eg http://www.example.com/{fragment}
) 开头,其他资源以定义的前缀 (Eg example:{fragment}
) 开头。两个片段代表同一个对象,因此必须对所有出现的前缀片段和 URL 片段进行任何更改,反之亦然。
这是一个例子:
每次http://www.example.com/Example_1
orexample:Example_1
出现时,我都想替换Example_1
文件中所有出现的片段,对于 UUID(例如186e4707_afc8_4d0d_8c56_26e595eba8f0
),导致所有出现都被替换为http://www.example.com/186e4707_afc8_4d0d_8c56_26e595eba8f0
or example:186e4707_afc8_4d0d_8c56_26e595eba8f0
。
这需要为文件中的每个唯一片段发生,这意味着 的 UUID 不同Example_2
,Example_3
依此类推。
到目前为止,我已经设法找到这行 Regex:(((?<=### http:\/\/archive\.semantyk\.com\/).*)|(?<=archive:)([^\s]+))
用于识别片段,但我真的被替换部分卡住了。
我相信这不是一个难题,但我确实认识到它的复杂性。
我希望我能很好地解释自己,但如果我没有请让我知道。
你知道如何解决这个问题吗??
非常感谢您阅读本文。
编辑:
我尝试使用 re.sub 使用此输入:
### http://archive.semantyk.com/Abbreviation
archive:Abbreviation rdf:type owl:Class ;
rdfs:subClassOf archive:Word .
### http://archive.semantyk.com/Ability
archive:Ability rdf:type owl:Class ;
rdfs:subClassOf archive:Quality .
它产生了这个结果:
### http://archive.semantyk.com/4f5b99bb_2bff_4166_8468_0134a1d864ae
archive:4f5b99bb_2bff_4166_8468_0134a1d864ae rdf:type owl:Class ;
rdfs:subClassOf archive:4f5b99bb_2bff_4166_8468_0134a1d864ae .
### http://archive.semantyk.com/4f5b99bb_2bff_4166_8468_0134a1d864ae
archive:4f5b99bb_2bff_4166_8468_0134a1d864ae rdf:type owl:Class ;
rdfs:subClassOf archive:4f5b99bb_2bff_4166_8468_0134a1d864ae .
但这是不正确的,因为 UUID 相同但资源(片段)不同。
有任何想法吗?
编辑:解决了!
xcan的代码解决了它!我只是做了一些调整让它工作。
这是最终的代码:
import re
import uuid
def generateUUID():
identifier = uuid.uuid4().hex
identifier = identifier[0:8] + '_' + identifier[8:12] + '_' + identifier[12:16] + '_' + identifier[16:20] + '_' + identifier[20:]
print('Generated UUID: ' + identifier)
return identifier
def main():
text = open('{path}', 'r').read()
# Firsts find what needs to changed.
rg = r"archive:([^\s]+)"
matches = re.findall(rg, text, re.M)
# convert list to a set to get rid of repeating matches
# then convert back to a list again
unique_matches = list(set(matches))
# Change unique words with unique uuids. Same word won't get a
# different uuid
for match in unique_matches:
pattern = r"(?<=archive:)(" + match + ")"
text = re.sub(pattern, str(generateUUID()), text)
file = open('{path}', 'w')
file.write(text)
file.close()
main()
您只需要将 {path} 替换为文件的路径即可!希望这对你也有效。
干杯!
解决方案
repl
您可以使用参数将函数传递给 re.sub,如此处所示。因此,您可以使用自己的一套规则来处理每场比赛。
编辑
根据评论编辑。archive:..
找到匹配项然后一一替换,因此位于文件中不同位置的相同匹配项获得相同的 uuid。
import uuid
import re
def main():
text = """ ### http://archive.semantyk.com/Abbreviation
archive:Abbreviation rdf:type owl:Class ;
rdfs:subClassOf archive:Word .
### http://archive.semantyk.com/Ability
archive:Ability rdf:type owl:Class ;
rdfs:subClassOf archive:Quality .
### http://archive.semantyk.com/Abbreviation
archive:Abbreviation rdf:type owl:Class ;
rdfs:subClassOf archive:Word ."""
# Firsts find what needs to changed.
rg = r"archive:([^\s]+)"
matches = re.findall(rg, text, re.M)
# convert list to a set to get rid of repeating matches
# then convert back to a list again
unique_matches = list(set(matches))
# Change unique matches with unique uuids. Same matches won't get a
# different uuid
for match in unique_matches:
pattern = r"(?<=archive:)(" + match + ")"
text = re.sub(pattern, str(uuid.uuid4()), text)
print(text)
if __name__ == "__main__":
main()
推荐阅读
- xamarin - 设置所有视图的辅助颜色
- websocket - 将可执行标准输出管道传输到 websocket
- python - 如何避免图像中的两个对象重叠
- python - 向 GUI 标签添加滚动条不起作用
- apache-spark - Apache spark 主服务器未启动。引起:java.lang.reflect.InaccessibleObjectException
- c - 关于未初始化局部变量的警告
- python - Pygame 让游戏因延迟而无法玩
- arrays - 如何在 mongoDB 中对多个 $addToSet 数组进行排序?
- java - 赫罗库。没有运行 Web 进程
- python - PyQt5 - 添加到垂直框布局时不显示小部件