newbie - Regular expression help

Yann Schwartz yann at winwise.fr
Sun Nov 19 17:45:36 CET 2000


On Sun, 19 Nov 2000 23:09:00 +0900, "June Kim"
<junaftnoon at nospamplzyahoo.com> wrote:

>
>"Satheesh Babu" <vsbabu at erols.com> wrote in message
>news:8v8i2a$lmt$1 at bob.news.rcn.net...
>> Hi all,
>>
>> I need to scan a string and replace all occurrences of
>> "./" with null. But "../" shouldn't get changed.
>>
>> Stupid way is to first replace ../ with AbRaCaDaBrA
>> and then replace ./ with null and then replace
>> AbRaCaDaBrA with ../
>>
>> There should be a simpler solution.
>>
>
>One simpler solution may be,
>
>>>> pat=re.compile(r'[^.](\./)')
>>>> pat.sub('',s)

Actually, no.

If you do this, the leading character [^.] will be chopped of as well.
And this regexp doesn't treat the case of a leading ./

The regexp should be

>>> pat=re.compile(r'([^.]|^)(\./)')
>>> pat.sub('\\1',s)

(there may be tricky cases overlooked here, but it seems to work).



More information about the Python-list mailing list