Python Regex: Using a lookahead
<p>I'm trying to detect text of the following type, in order to remove it from the text:</p>

<pre><code>BOLD:Parshat NoachBOLD:
BOLD:Parshat Lech LechaBOLD:
BOLD:Parshat VayeraBOLD
BOLD:Parshat ShâminiBOLD:
</code></pre>

<p>But only to capture this part:</p>

<pre><code>BOLD:Parshat Noach
BOLD:Parshat Lech Lecha
BOLD:Parshat Vayera
BOLD:Parshat Shâmini
</code></pre>

<p>I thought to use this regex using lookahead:</p>

<pre><code>re.sub(r"BOLD:Parshat .*?(?=(:BOLD))","",comment) #tried lookahead with and without parens
</code></pre>

<p>But it doesn't seem to be detecting them. What might be the problem? The text is followed by a snippet in Hebrew, not sure if that is what is causing the problem.</p>

<p>Please note that these segments are embedded in the middle of different lines, as mentioned followed by a Hebrew snippet.</p>

<p>In python you can just do:</p>

<pre><code>str = re.sub(r'BOLD:?$', '', str, 0, re.MULTILINE)
</code></pre>

<p><a href="https://regex101.com/r/qiNd4L/3" rel="nofollow">RegEx Demo</a></p>

<p>That will remove <code>BOLD</code> followed by optional <code>:</code> from the end of each line.</p>

<hr>

<p><strong>EDIT:</strong> If this <code>BOLD:</code> term is not always at end of line, one can use:</p>

<pre><code>&gt;&gt;&gt; print re.sub(r'\b(BOLD:.*)BOLD:?', r'\1', str)
BOLD:Parshat Noach
BOLD:Parshat Lech Lecha
BOLD:Parshat Vayera
BOLD:Parshat Shâmini
</code></pre>

python regex