因此,我有一堆长字符串,因此想出一种有效的方法来执行此操作,假设我有一个字符串,如下所示
"< stuff to remove> get this stuff <stuff to remove>所以,我正在试着提取"get this this“
所以我正在写一些类似这样的东西。
strt_pos = 0
end_pos = 0
while True:
strt_idx = string.find(start_point, strt_pos) # start_point = "<" in our example
end_idx = string.find(end_point, end_pos) # end_point = ">" in our example
chunk_to_remove = string[strt_idx:end_idx]
# Now how do i chop this part off from the string??
strt_pos = strt_pos + 1
end_pos = end_pos + 1
if str_pos >= len(string) # or maybe end_pos >= len(string):
break实现这一点的更好方法是什么?
发布于 2012-04-11 01:11:59
正则表达式是一种简单的方法(尽管不一定像jedwards的答案所示的那样更快):
import re
s = '< stuff to remove> get this stuff <stuff to remove>'
s = re.sub(r'<[^>]*>', '', s)在此s之后将是字符串' get this stuff '。
https://stackoverflow.com/questions/10093186
复制相似问题