[英]How can you use re.sub to replace the content of a capture group with a string?
我正在尝试确定如何使用re.sub
替换捕获组的内容,但是不幸的是,我的大脑太小,无法理解re.sub
函数的API文档 。
到目前为止,我已经成功地使用re.search
函数成功隔离了我想替换的字符串,但是使用re.sub
函数的API的过程正确地摆脱了我过去,现在和将来的能力,并且悲剧性的大脑。
我可以使用re.search
模块选择要替换的字符串:
import re
RE_SELECT_CURSOR = re.compile(r'.*\(.*after:\s*(?:"*)([A-Za-z0-9\+\/\=]+)(?:"*)\s*\).*', flags=re.MULTILINE)
query = """
{{
users(id: "{}") {{
things(first: {}, after: null){{
pageInfo {{
startCursor endCursor hasNextPage
}}
edges {{
node {{
id
}}
}}
}}
}}
}}
"""
#: Identifying the string that I would like to replace (i.e. the "cursor").
matches = re.search(RE_SELECT_CURSOR, query)
if matches:
cursor = matches.group(1)
print(cursor)
但是,一旦我尝试用hello
替换null
,我的naïveté就变得显而易见。
#: Trying to replace the "cursor".
result = re.sub(RE_SELECT_CURSOR, "hello", query)
print(result)
结果如下:
{{
users(id: "{}") {{
hello
pageInfo {{
startCursor endCursor hasNextPage
}}
edges {{
node {{
id
}}
}}
}}
}}
}}
我尝试了其他方法,但没有一个起作用-正确地使用re.sub
非常痛苦,但是,在回顾了几十个示例之后,我的大脑根本没有能力来理解这一点。
一种这样的方法如下,但是,这可笑是不正确的,我知道我应该为自己的“尝试”感到尴尬。
RE_REPLACE_GROUP = '.*\(.*after:\s*(?:"*)("hello")(?:"*)\s*\).*'
result = re.sub(RE_SELECT_CURSOR, RE_REPLACE_GROUP, query)
另一种方法如下,但这也是可笑的错误。
import re
RE_SELECT_CURSOR = re.compile(r'.*\(.*after:\s*(?:"*)([A-Za-z0-9\+\/\=]+)(?:"*)\s*\).*', flags=re.MULTILINE)
query = """
{{
organization(id: "{}") {{
assets(first: {}, after: null){{
pageInfo {{
startCursor endCursor hasNextPage
}}
edges {{
node {{
id
}}
}}
}}
}}
}}
"""
#: Identifying the string that I would like to replace (i.e. the "cursor").
matches = re.search(RE_SELECT_CURSOR, query)
if matches:
cursor = matches.group(1)
query = query.replace("after: {}".format(cursor), "after: {}".format("hello"))
print(query)
结果是:
{{
organization(id: "{}") {{
assets(first: {}, after: hello){{
pageInfo {{
startCursor endCursor hasNextPage
}}
edges {{
node {{
id
}}
}}
}}
}}
}}
从技术上讲,此结果是正确的,但不会容忍错误位置中的空格。
有什么我可以用hello
替换null
吗?
使用group(1)
将提取您感兴趣的组,但是当使用sub
,匹配项将完全由替换项代替。 并且由于表达式的开始和结尾用于匹配上下文,因此在求和时,整个匹配将替换为hello
; 不只是您感兴趣的人群。
一种方法是创建3个组,并在比赛时将组替换为原始组\\1
和\\3
并通过hello
更改中央组,如下所示:
import re
RE_SELECT_CURSOR = re.compile(r'(\(.*after:\s*"*)([A-Za-z0-9\+\/\=]+)("*\s*\))') # you don't need MULTILINE flag or leading/trailing .* patterns
query = """
{{
users(id: "{}") {{
things(first: {}, after: null){{
pageInfo {{
startCursor endCursor hasNextPage
}}
edges {{
node {{
id
}}
}}
}}
}}
}}
"""
result = re.sub(RE_SELECT_CURSOR, r"\1hello\3", query)
print(result)
印刷品:
{{
users(id: "{}") {{
things(first: {}, after: hello){{
pageInfo {{
startCursor endCursor hasNextPage
}}
edges {{
node {{
id
}}
}}
}}
}}
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.