简体   繁体   English

如何删除两个特定的chaster

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

相关问题 如何删除文件的特​​定最后两行? - How to remove specific last two lines of a file? 如何使用字母数字字符从单个列表创建元组? - How to create tuples from a single list with alpha-numeric chacters? 如何删除一个带有两个特定首字母的句子 - how to remove one sentence with two specific initial words 如何删除文本中包含两个特定字符串的行? - How to remove a line containing two specific string in text? 如何通过 python 删除 dataframe 列中两个特定单词之间的文本 - How to remove text between two specific words in a dataframe column by python 如何删除或替换两个xml标签之间的特定字符[linux,python,lxml,sed,awk,...]? - how to remove or replace specific chars are between two xml tags [linux, python, lxml, sed, awk,…]? 如何从python中的给定字符串中删除两个子字符串之间的特定字符串? - How to remove specific string between two substrings from given string in python? 如果两个列表超过给定的欧氏距离,如何从它们中删除特定值? - How to remove a specific value from two lists if they exceed a given Euclidean distance? 如何删除具有特定字符的句子? - How to remove sentences with a specific character? 如何删除列表中特定元素的最后两个字符? - How can I remove the last two characters of a specific element in my list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM