[Tutor] extend my re
Ricardo Aráoz
ricaraoz at gmail.com
Sat Nov 22 19:22:38 CET 2008
Marco Catunda wrote:
> Ricardo,
>
> Try this pattern
>
> MyPattern = re.compile(r'(.*?Start.*?)((^.*?comment.*?)*)(^.*?comment)(.*?end)',
> re.S | re.M )
>
>
Almost did it. But if there was something after and in the same line of
the "comment" I want commented it wouldnt be taken.
from :
comment xxx
to :
/* comment*/ xxx
and I wanted :
/* comment xxx*/
But it helped a lot, I finally got :
>>> MyPattern =
re.compile(r'(.*?Start.*?)((^.*?comment.*?)*)(^.*?comment.*?$)(.*?end)',
re.S | re.M )
which did the trick.
Thanks a lot Marco!
> On Sat, Nov 22, 2008 at 12:52 PM, Ricardo Aráoz <ricaraoz at gmail.com> wrote:
>
>> Hi, I've got a half working re but I can't find a way to give it the
>> final touch.
>> Let's say I have (it would actually be source code file) :
>>
>>>>> import re
>>>>> MyString = """Algo
>>>>>
>> ... Start
>> ... otro
>> ... comment
>> ... otro
>> ... comment
>> ... comment
>> ... otro
>> ... end
>> ... """
>>
>>>>> MyPattern =
>>>>>
>> re.compile(r'(.*?Start.*?)((comment.*?)*)(comment)(.*?end)', re.S)
>>
>>
>>>>> print MyString
>>>>>
>> Algo
>> Start
>> otro
>> comment
>> otro
>> comment
>> comment
>> otro
>> end
>>
>>
>>>>> print MyPattern.sub(r'\1\2/*\4*/\5', MyString)
>>>>>
>> Algo
>> Start
>> otro
>> comment
>> otro
>> comment
>> /*comment*/
>> otro
>> end
>>
>> Which is basically ok. I have to find the last "comment" in the block
>> and comment it. But I'd like to comment the whole line, my desired
>> output would be :
>> Algo
>> Start
>> otro
>> comment
>> otro
>> comment
>> /* comment*/
>> otro
>> end
>>
>> And if there was something after the "comment" I would also like it to
>> be commented :
>> from:
>> comment and something else
>> to :
>> /* comment and something else*/
>>
>> any help?
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>
More information about the Tutor
mailing list