[英]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: 脚步:
camel
to dog
camel
替换为dog
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.