[Tutor] search function

Kent Johnson kent_johnson at skillsoft.com
Fri Jul 30 15:17:21 CEST 2004


Conrad,

Regular expressions are your friends if you get to know them! You can do 
what you want with just one RE using subgroup matches. I don't know if it's 
more Pythonic, but it's more concise and a better use of REs. A more 
Pythonic way to iterate the config file is to use "for line in config_file".

Say you want to search for two variables, v1 and v2. Then you can use this RE:
r'"(v1|v2)"="([^"]*?)"$'

The first group (v1|v2) will match the actual variable name and the second 
group ([^"]*?) will match the value. The quotes are outside the parentheses 
so they won't be included in the groups.

(BTW I think this will work just as well: r'"(v1|v2)"="([^"]*)"' - I left 
in the ? and $ as they were in your original post)

You could also allow for white space around the = by adding \s* on each 
side of it: r'"(v1|v2)"\s*=\s*"([^"]*)"' .

One final note: If your input string with the variable names could have 
spaces after the commas, you might want to use re.split() to split it, or 
if you have control over the calling function maybe pass it as a list of 
names or a space-delimited string. Or use a combination of split, strip and 
join like this:
     altVars = '|'.join([s.strip() for s in variables.split(',')])


Below is a complete program with the re-written function and a simple test 
harness.

Kent

#####################################

import re

def display_var(config_file, variables):
     altVars = variables.replace(',','|')
     var_disp = re.compile(r'"(%s)"="([^"]*?)"$' % altVars)

     for line in config_file:
         match = var_disp.search(line)
         if match:
             print "%s: %s" % match.group(1, 2)


testData = '''
     "var3"="33"
     "var1"="value1"
     "var2"="22"
     "dontcare"="42"
'''

vars = 'var1,var2,var3'

import StringIO
config = StringIO.StringIO(testData)

display_var(config, vars)

At 10:55 AM 7/29/2004 -0700, Conrad wrote:
>I'm writing a script that extracts values from config files in the form
>of:
>
>"variable"="value"
>"variable1"="value"
>
>I wrote this function, which takes a comma delimited list of variables
>to search for. (variable1,variable2,....), and then searches through
>each line of the config file, checking if the variable is there, and
>then printing out the variable found and the value:
>
>def display_var(variables):
>         var_disp = re.compile(r'%s' % (variables.replace(',','|')))
>         line = config_file.readline()
>         var_value = re.compile(r'"[^"]*?"$')
>         var_name = re.compile(r'".*?"')
>         while line != '':
>                 if var_disp.search(line):
>                         value = var_value.search(line)
>                         var = var_name.search(line)
>                         print "%s: %s" % (var.group()[1:-1],
>value.group()[1:-1])
>                 else:
>                         pass
>                 line = config_file.readline()
>
>There are a few things that are bugging me about this. One is the heavy
>use of regular expressions, and two is that im using [1.-1] to strip the
>qoutes. Can anyone point out how to make this more pythonic?
>
>Your time is appreciated,
>         Conrad
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list