簡體   English   中英

如何剝離[]中的所有內容

[英]How to strip of everything in []

我試圖刪除[]內的主要文本,包括[] ,如下所示

title  = "[test][R] D123/Peace123456: panic:"
print title
title = title.strip('[.*]')
print title

輸出: -

test][R] D123/Peace123456: panic:

預期產量:

[R] D123/Peace123456: panic:

你需要非貪婪的Regex來匹配first []從開始,而re.sub來進行替換:

In [10]: title  = "[test][R] D123/Peace123456: panic:"

# `^\[[^]]*\]` matches `[` followed by any character
# except `]` zero or more times, followed by `]`
In [11]: re.sub(r'^\[[^]]*\]', '', title)
Out[11]: '[R] D123/Peace123456: panic:'

# `^\[.*?\]` matches `[`, followed by any number of
# characters non-greedily by `.*?`, followed by `]`
In [12]: re.sub(r'^\[.*?\]', '', title)
Out[12]: '[R] D123/Peace123456: panic:'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM