How to add a variable into my re.compile expression

python, python-2.7, regex, variables

Solution

You can do it like so...

>>> regex2 = re.compile('.*(%s).*'%what2look4)

Or you can use format:

>>> regex2 = re.compile('.*({}).*'.format(what2look4))

Problem

So I am trying to look through a file for a keyword that is represented by the variable `what2look4`. Whenever I run this program however it keeps returning blank data. The code is as follows: ``` regex2=re.compile(".*(what2look4).*") ``` I believe the problem is that the file is being searched for `what2look4` as a string in itself instead of what that variable represents. Please correct me if I'm wrong, thanks for the help.

Original source