[英]How to remove or add sentences that begin with a specific char or string?
我有如下字符串:
$ CHECK(varlen->get_len() >= HOME_LEN);
$
@ //add more sentences
@ /* some comments */
@ if(varlen->get_len() >= HOME_LEN)
@ {
我想刪除以$或@開頭的行,具體取決於需要。 我嘗試使用以下命令:拆分給定字符串,並遍歷列表中的每個元素以檢查它們是否以$
開頭
for sentence in string.split("\n"):
print(re.sub(r"\b$ \\w-", "", sentence))
但這沒有用。
您可以使用str.startswith()
:
for line in string.split("\n"):
if not line.startswith('$'):
print(line)
這是檢查字符串是否以另一個字符串開頭的Python方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.