<p>我要删除给定文件中以*开头的所有行。例如,以下内容:</p>
<pre><code>* This needs to be gone
But this line should stay
*remove
* this too
End
</code></pre>
<p>应产生:</p>
<pre><code>But this line should stay
End
</code></pre>
<hr/>
<p>我最终需要做的是:</p>
<ol>
<li>删除圆括号和方括号内的所有文本(包括圆括号/方括号)</li>
<li>如上所述,删除以“”开头的行。</li>
</ol>
<p>到目前为止,我已经能够用以下语句来处理#1:<code>re.sub(r'[.?]|(.*?)', '', fileString)</code>。我试了几次,但最后总是把我不想拿走的东西拿走</p>
<hr/>
<p><strong>解决方案1(无regex)</strong></p>
<pre><code>>>> f = open('path/to/file.txt', 'r')
>>> [n for n in f.readlines() if not n.startswith('*')]
</code></pre>
<p><strong>解决方案2(regex)</strong></p>
<pre><code>>>> s = re.sub(r'(?m)^\*.*\n?', '', s)
</code></pre>
<p>谢谢大家的帮助。</p>