regexp help

John Machin sjmachin at lexicon.net
Fri May 9 21:39:59 EDT 2008


globalrev wrote:
> ty. that was the decrypt function. i am slo writing an encrypt
> function.
> 
> def encrypt(phrase):
>     pattern =
> re.compile(r"([bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ])")

The inner pair of () are not necessary.

>     return pattern.sub(r"1\o\1", phrase)
> 
> doesnt work though, h becomes 1\\oh.

To be precise, "h" becomes "1\\oh", which is the same as r"1\oh". There 
is only one backslash in the result.

It's doing exactly what you told it to do: replace each consonant by
(1) the character '1'
(2) a backslash
(3) the character 'o'
(4) the consonant

> 
> 
> def encrypt(phrase):
>     pattern =
> re.compile(r"([bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ])")
>     return pattern.sub(r"o\1", phrase)
> 
> returns oh.

It's doing exactly what you told it to do: replace each consonant by
(1) the character 'o'
(2) the consonant

> i want hoh.

So tell it to do that:
return pattern.sub(r"\1o\1", phrase)

> i dont quite get it.why cant i delimit pattern with \

Perhaps you could explain what you mean by "delimit pattern with \".




More information about the Python-list mailing list