Jerome Jabson wrote:
> Hello,
> 
>  
> 
> I’m having trouble trying to pass a variable into
> re.compile(), using the “r” option. Let’s say I
> have the following:
> 
>  
> 
> import re
> 
> arg1 = ‘10”
> 
> p = re.compile(r + arg1 + '\.(\d+\.\d+\.\d+)')
> 
>  
> 
> This works if I do:
> 
>  
> 
> p = re.compile(r ‘10\.(\d+\.\d+\.\d+)')
> 
>  
> 
> Is there something special I need to do for the
> “r” option? Like escape it when using a variable?

'r' is not an option to re.compile(), it is a modifier for the string 
itself that affects how the string is parsed. So you can use
   p = re.compile(arg1 + r'\.(\d+\.\d+\.\d+)')

or use string substitution as John suggests.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to