簡體   English   中英

您如何使用re.sub用字符串替換捕獲組的內容?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM