简体   繁体   English

Python-用正则表达式中的字符串替换字符串EXCEPT的所有实例

[英]Python - Replace all instances of string EXCEPT for those in regex

I would like to replace all mentions of the string "camel" except when it occurs in the context of the below in bold: 我想替换字符串“ camel”的所有提及,除非它出现在以下以粗体显示的上下文中:

Source{[Name="Camel"]} 

In that case I would like to leave it as is. 在这种情况下,我想保留原样。 I have a regex which can identify this format: 我有一个可以识别这种格式的正则表达式:

Source{\[Name=\"(\w+)\"\]}

So for example: 因此,例如:

let
    Source=#"Middle East",
    Camel = Source{[Name="Camel"]}[Content],
    #"Invoke Custom Function1" = Table.AddColumn(Camel)
    #"Removed Other Columns1" = Table.SelectColumns(#"Invoke Custom 
    Function1", {"Camel"})

should be changed to: 应该更改为:

let
        Source=#"Middle East",
        Dog = Source{[Name="Camel"]}[Content],
        #"Invoke Custom Function1" = Table.AddColumn(Dog)
        #"Removed Other Columns1" = Table.SelectColumns(#"Invoke Custom 
        Function1", {"Dog"})

steps: 脚步:

  1. Replace all occurrences of camel to dog 将所有出现的camel替换为dog
  2. Replace all occurrences of source{[name="dog"]} to source{[name="camel"]} 将所有出现的source{[name="dog"]}替换为source{[name="camel"]}

inefficient but will do the trick 效率低下,但会成功

You may use 您可以使用

re.sub(r'(Source{\[Name="\w+"]})|\bCamel\b', lambda x: x.group(1) or 'Dog', text)

See the Python demo 参见Python演示

Details 细节

  • (Source{\\[Name="\\w+"]})|\\bCamel\\b - matches and captures into Group 1 the pattern you have ( Source{[Name="some_word123"}} like string) or will match a Camel whole word (Source{\\[Name="\\w+"]})|\\bCamel\\b匹配并将您拥有的模式捕获到组1中( Source{[Name="some_word123"}}像字符串一样),或者匹配整个Camel
  • lambda x: x.group(1) or 'Dog' callable will put back the text in Group 1 if Group 1 matched, else, the word Camel will be replaced with Dog . lambda x: x.group(1) or 'Dog'如果第1组匹配,则lambda x: x.group(1) or 'Dog'可调用项将放回第1组中的文本,否则,将Camel替换为Dog

The key insight here is that no part of the string can match twice. 这里的关键见解是字符串的任何部分都不能匹配两次。 So if Source{[Name="Camel"]} matches, Camel inside it can't match. 因此,如果Source{[Name="Camel"]}匹配,则其中的Camel无法匹配。 So we make regexp an alternation, and check if we matched the part that we wanted to replace, otherwise we just return what we found, unchanged: 因此,我们将regexp进行了替换,并检查是否与要替换的部分匹配,否则,我们将返回找到的内容不变:

import re
camel_re = re.compile(r"Source\{\[Name=\"Camel\"\]\}|(?P<replacee>Camel)")

def camel_replacer(m):
    if m.group('replacee'):
        return "Dog"
    else:
        return m.group()

camel_re.sub(camel_replacer, text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python正则表达式 - 替换除大括号之外的所有字符 - Python regex - Replace all characters except those between braces 将除第一行之外的所有行中的字符串替换为 Python - Replace string in all rows except the first with Python Python-替换字符串中的所有单词,除了某些单词 - Python - replace all words in a string except some 拆分字符串并捕获 python 正则表达式中的所有实例 - Split a string and capture all instances in python regex python中的正则表达式,替换单词中除#hashtag和@username之外的所有元音 - regex in python, replace all vowels in word except #hashtag and @username Python正则表达式替换所有模式,除非它紧挨着重复模式 - Python regex replace all patterns except when it is next to a repeated pattern 尝试正则表达式所有大写单词,除了那些紧跟在 Python 中的单词 - Trying to regex all capitalized words EXCEPT those immediately following a period in Python Python正则表达式:重命名名称中除具有今天日期的文件以外的所有文件 - Python regex: rename all files except those with today's date in the name Python-正则表达式查找字符串中的所有匹配项并替换 - Python - Regex find all matches in string and replace python regex用匹配的字符串替换所有出现的事件 - python regex replace all occurances with the matched string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM