Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式:用匹配项的ID替换匹配项_Python_Regex - Fatal编程技术网

Python 正则表达式:用匹配项的ID替换匹配项

Python 正则表达式:用匹配项的ID替换匹配项,python,regex,Python,Regex,我遇到了这种情况,我想知道我是否可以用正则表达式: 我有一个以下格式的字符串: {{We all}} love {{stackoverflow}}. 我的问题是如何使用regex replace获得: match1 love match2 试试这个 result = re.sub("([{]{2}[^}]+[}]{2})([^{]+)([{]{2}[^}]+[}]{2})", r"match1\2match2", subject) 解释 """ ( # Match the

我遇到了这种情况,我想知道我是否可以用正则表达式:

我有一个以下格式的字符串:

{{We all}} love {{stackoverflow}}.
我的问题是如何使用regex replace获得:

match1 love match2

试试这个

result = re.sub("([{]{2}[^}]+[}]{2})([^{]+)([{]{2}[^}]+[}]{2})", r"match1\2match2", subject)
解释

"""
(          # Match the regular expression below and capture its match into backreference number 1
   [{]        # Match the character “{”
      {2}        # Exactly 2 times
   [^}]       # Match any character that is NOT a “}”
      +          # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   [}]        # Match the character “}”
      {2}        # Exactly 2 times
)
(          # Match the regular expression below and capture its match into backreference number 2
   [^{]       # Match any character that is NOT a “{”
      +          # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(          # Match the regular expression below and capture its match into backreference number 3
   [{]        # Match the character “{”
      {2}        # Exactly 2 times
   [^}]       # Match any character that is NOT a “}”
      +          # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   [}]        # Match the character “}”
      {2}        # Exactly 2 times
)
"""

适用于任何数量的组。

假设在要替换的项目之外没有其他花括号,这应该是一件简单的事情。拿起一两本正则表达式教程,看看一些文档。我正在使用Python。将尝试查看一些建议的教程:)
s = '{{We all}} love {{stackoverflow}}.' #string to match
pat = re.compile(r'\{\{.*?\}\}') #pattern
#now replace each matched group by its index
for index,group in enumerate(re.findall(pat,s)):
    s = re.sub(group, 'match'+str(index+1), s)