从 python 中的给定字符串中删除特定模式
removing a particular pattern from a given string in python
我想从字符串 "[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
生成以下输出
预期输出
foo foo foo \r\n\r\n\r\n
所以 - 删除所有 [cid:...]
块和所有 newlines/carriage-returns 尾随它们?
>>> import re
>>> s = "[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
>>> re.sub(r"\[cid:(.+?)\][\r\n]*", "", s)
'foo foo foo \r\n\r\n\r\n'
您可以试试这个正则表达式搜索
import regex as re
x = r"[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
re.search("\]([^]]+)\[", x)[1]
首先我们将导入正则表达式
我们将制作字符串 - 原始字符串
x = r"" -> the r before the string
-> we will get the next result with raw string
foo foo foo \r\n\r\n\r\n
-> we will get the next result without raw string
foo foo foo
...
我们进行正则表达式搜索以获取]和[之间的文本,re.search方法return一个匹配对象,匹配对象包含2个项目。
re.search("\]([^]]+)\[", x)[0]
-> first one with ] and [
re.search("\]([^]]+)\[", x)[1]
-> second one without ] and [
我想从字符串 "[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
预期输出
foo foo foo \r\n\r\n\r\n
所以 - 删除所有 [cid:...]
块和所有 newlines/carriage-returns 尾随它们?
>>> import re
>>> s = "[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
>>> re.sub(r"\[cid:(.+?)\][\r\n]*", "", s)
'foo foo foo \r\n\r\n\r\n'
您可以试试这个正则表达式搜索
import regex as re
x = r"[cid:12d32323232dde]foo foo foo \r\n\r\n\r\n[cid:123fsr3ef234fsdfere]\r\n"
re.search("\]([^]]+)\[", x)[1]
首先我们将导入正则表达式
我们将制作字符串 - 原始字符串
x = r"" -> the r before the string -> we will get the next result with raw string foo foo foo \r\n\r\n\r\n -> we will get the next result without raw string foo foo foo ...
我们进行正则表达式搜索以获取]和[之间的文本,re.search方法return一个匹配对象,匹配对象包含2个项目。
re.search("\]([^]]+)\[", x)[0] -> first one with ] and [ re.search("\]([^]]+)\[", x)[1] -> second one without ] and [