tags:

views:

193

answers:

1

I'm wondering if its possible to use string substitution along with the python re module?

For example I'm using optparse and have a variable named options.hostname which will change each time the user executes the script.

I have the following regex matching 3 strings in each line of the log file.

 match = re.search (r'^\[(\d+)\] (SERVICE NOTIFICATION:).*(\bCRITICAL)', line)

I want to be able to perform string substitution by matching options.hostname as the last match group however I can't get any variations to work. Is this possible?

 match = re.search (r'^\[(\d+)\] (SERVICE NOTIFICATION:).*(\bCRITICAL).*(s%), line) % options.hostname
+2  A: 
 match = re.search (r'^\[(\d+)\] (SERVICE NOTIFICATION:).*(\bCRITICAL).*(%s)'
                    % options.hostname, line)
Alex Martelli
Small typo: I believe you meant "%s" instead of "s%".
Benji York
Ah great thank you. I had the formatting backwards!Cheers Alex
Joshua
@Benji tx, good spotting, editing now to fix. @Joushua, glad to have been helpful (remember to accept an answer when -- but only when -- you know it works!-)
Alex Martelli