regexp substitution - a lot of work!

Lukas Holcik xholcik1 at fi.muni.cz
Wed Jun 16 18:34:33 CEST 2004


Yes, sorry, I was in such a hurry I didn't found it in the documentation, 
but when I want to use a lot of very different expressions using a lot of 
different grouping, which would be easy to implement using s/(..)/x\1x/ 
then it is quite problematic having to use re.sub(), isn't it?

for example these perlish expressions:
     's/^<i>.*?</i>\s*//'
     's/<b>\s*</b><br>.*//s'
     's/^(?:<[^>]>)?\t(.*?)<br>/<p>\1</p>/'
     's/\t+| {2,}/ /'
     's/(<[^/][^>]*>)([^\n])/\1\n\1/'
     's/(?<!\n)(</[^>]*>)/\n\1/'
     's/ /\n/'
couldn't it be easier to call external perl (or sed) ? Thanks,

---------------------------------------_.)--
|  Lukas Holcik (xholcik1 at fi.muni.cz)  (\=)*
----------------------------------------''--

On Wed, 16 Jun 2004, Tuure Laurinolli wrote:

> Lukas Holcik wrote:
>
>
>
>> That is quite a lot of work! Don't you know some better and easier way?
>> Thanks in advance,
>
> Why not use re.sub?
>
>>     regexp = re.compile(..)
>>     result = regexp.search(data)
>>     while result:
>>         data = data[:result.start()] + .. result.group(..) + \
>>             data[result.end():]
>>         result = regexp.search(data)
>> 
>>     ... for one regexp substitution
>
>
> regexp = re.compile(a_pattern)
> result = regexp.sub(a_replacement, data)
>
> Or use a callback if you need to modify the match:
>
> def add_one(match):
>    return match.group(1) + '1'
>
> regexp = re.compile(a_pattern)
> result = regexp.sub(add_one, data)
>



More information about the Python-list mailing list