regexp help

Matimus mccredie at gmail.com
Fri May 9 19:06:17 EDT 2008


On May 9, 3:19 pm, globalrev <skanem... at yahoo.se> wrote:
> i want to a little stringmanipulationa nd im looking into regexps. i
> couldnt find out how to do:
> s = 'poprorinoncoce'
> re.sub('$o$', '$', s)
> should result in 'prince'
>
> $ is obv the wrng character to use bu what i mean the pattern is
> "consonant o consonant" and should be replace by just "consonant".
> both consonants should be the same too.
> so mole would be mole
> mom would be m etc

>>> import re
>>> s = s = 'poprorinoncoce'
>>> coc = re.compile(r"(.)o\1")
>>> coc.sub(r'\1', s)
'prince'

Matt



More information about the Python-list mailing list