[英]How to remove two specific chacters
I have the following text: 我有以下文字:
rule="Aa&~a->c"
and I need to remove the letter a that is alone with the ~ sign so far I tried this regex expression: 到目前为止,我需要删除带〜符号的字母a,我已经尝试过此正则表达式:
auxRegla=re.sub(r"(?<!~)\ba\b",r"",regla)
the \\ba\\b
are to set the boundaries and that works just fine and the (?<!~)
has the intention to remove the ~ sign, but those two togethere aren't doing anything. \\ba\\b
设置边界,并且效果很好,并且(?<!~)
打算删除〜符号,但是那两个在一起并没有做任何事情。
I need the result to be: 我需要的结果是:
Aa&->c
You should not use a negative lookbehind here, since you do want to match ~
. 您不应该在这里使用负数,因为您确实想匹配
~
。 You can match ~a
directly instead, but with a word boundary assertion after a
: 您可以匹配
~a
,而不是直接的,但有一个单词边界断言后a
:
re.sub(r"~a\b", '', regla)
First of all, you are using lookbehind for ~
, which will match the thing inside, but will not consume it (to put it simply it will not remove the ~
, only a
's that are prepended with it). 首先,您使用的回顾后的
~
,这将匹配里面的东西,但不能消耗它(把它简单地把它不会删除~
,只有a
的是预先计划用它)。
Second, you are using the negative lookbehind, so your regex matches a
's NOT prepended with ~
. 其次,您使用的是负回顾后,让你的正则表达式匹配
a
“不是用前缀~
。
Better regex for you is ~a\\b
(no first word boundary \\b
since ~
is definitely not a word character, and a
definitely is, so it is always true) 更好的正则表达式,你是
~a\\b
(没有第一个字边界\\b
因为~
绝对不是一个单词字符,并且a
绝对,所以它始终是真实的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.