regex query

Robert Brewer fumanchu at amor.org
Thu Jun 10 17:14:27 CEST 2004


km wrote:
> how do i include variable as a part of the regex string ? - i 
> mean variable interpolation. like : 
> ##########code ##########
> #!/usr/bin/env python
> import re
> str = "abc"
> p = re.compile('str')  # defeats the purpose - literal string 'str'
> ##########code ##########
> any suggestions ? 

1. Don't use 'str' as a variable name; it's a builtin which you've just
clobbered (I couldn't resist ;)

2. Is there some reason you didn't try:

import re
atom = "abc"
p = re.compile(atom)


...?

Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list