擅长:python、mysql、java
<p>有点框架挑战,但“为什么要为团队而烦恼”?任何正则表达式都不匹配行尾(<code>$</code>),因此这里唯一的问题是确保<code>#</code>之后的所有内容都以单个<code>a-z</code>字符开头:</p>
<pre><code>import re
prefixes = {
r"http://www.w3.org/2002/07/owl#": r"owl:",
r"http://www.w3.org/1999/02/22-rdf-syntax-ns#": r"rdf:",
r"http://www.w3.org/2000/01/rdf-schema#": r"rdfs:",
r"http://schema.org/": "schema",
r"http://www.w3.org/2001/XMLSchema#": r"xsd:",
r"http://purl.org/linked-data/sdmx#": r"sdmx:",
r"http://www.w3.org/XML/1998/namespace": r"xml"
}
regex = re.compile("|".join(map(re.escape, prefixes.keys())))
test1 = "http://www.w3.org/XML/1998/namespace"
test2 = "http://www.w3.org/2000/01/rdf-schema#a"
assert regex.sub(lambda match:prefixes[match.group(0)], test1) == "xml"
assert regex.sub(lambda match:prefixes[match.group(0)], test2) == "rdfs:a"
</code></pre>