从文本字符串中删除2个字符之间的字符

时间:2022-01-30 20:24:49

I have text string like:

我有文字字符串,如:

"abcd[e]yth[ac]ytwec"

I need just

我需要

"abcdythytwec"

What is the easiest way to do it using regex or otherwise in python? I am using .split('[') method which is cumbersome.

在python中使用正则表达式或其他方法最简单的方法是什么?我使用的是.split('[')方法很麻烦。

2 个解决方案

#1


16  

In [11]: re.sub(r'\[.*?\]', '', 'abcd[e]yth[ac]ytwec')
Out[11]: 'abcdythytwec'

#2


2  

Try using re module:

尝试使用re模块:

import re

进口重新

re.sub(r'\[[^]]*\]', '', "abcd[e]yth[ac]ytwec")

re.sub(r'\ [[^]] * \]','',“abcd [e] yth [ac] ytwec”)

#1


16  

In [11]: re.sub(r'\[.*?\]', '', 'abcd[e]yth[ac]ytwec')
Out[11]: 'abcdythytwec'

#2


2  

Try using re module:

尝试使用re模块:

import re

进口重新

re.sub(r'\[[^]]*\]', '', "abcd[e]yth[ac]ytwec")

re.sub(r'\ [[^]] * \]','',“abcd [e] yth [ac] ytwec”)