简体   繁体   English

使用正则表达式删除特定单词之间的单词

[英]Using regular expression to delete words in between specific words

My data contains something like these:我的数据包含以下内容:

→ muching :酋長在這個距離的時候,北部山區都超過大豪雨標準了

推aitt :台東恆春間登陸不代表北台不會有強風.

→ teras: 7/7

I want my data to look like these:我希望我的数据看起来像这样:

酋長在這個距離的時候,北部山區都超過大豪雨標準了

台東恆春間登陸不代表北台不會有強風.

 7/7

I already tried some regex re.sub(r'^推:$', '', x) but I'm pretty sure this way is wrong.我已经尝试了一些正则表达式re.sub(r'^推:$', '', x)但我很确定这种方式是错误的。

does regex work with Chinese characters or → symbols?正则表达式是否适用于汉字或 → 符号?

You can try something like this, it will remove everything that starts in a line before the : and does work with Chinese characters:您可以尝试这样的事情,它会删除:之前的行中开始的所有内容,并适用于中文字符:

import re

txt = """
→ muching :酋長在這個距離的時候,北部山區都超過大豪雨標準了

推aitt :台東恆春間登陸不代表北台不會有強風.

→ teras: 7/7

"""

pattern = r'^.*:'
parsed_txt = re.sub(pattern, '', txt, flags=re.MULTILINE)

print(parsed_txt)
>>>"
酋長在這個距離的時候,北部山區都超過大豪雨標準了

台東恆春間登陸不代表北台不會有強風.

 7/7"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM