regexp questoin

Kent Johnson kent at kentsjohnson.com
Fri Jun 9 20:33:55 CEST 2006


micklee74 at hotmail.com wrote:
> hi
> 
> i created a script to ask user for an input that can be a pattern
> right now, i use re to compile that pattern
> pat = re.compile(r"%s" %(userinput) )  #userinput is passed from
> command line argument
> if the user key in a pattern , eg [-] ,  and my script will search some
> lines that contains [-]
> 
> pat.findall(lines)
> 
> but the script produce some error: sre_constants.error: unexpected end
> of regular expression
> 
> how can i successful catch  patterns such as "[-]" in my regexp
> compilation where input is unknown...?

Maybe you want
pat = re.compile(re.escape(userinput))

Kent



More information about the Python-list mailing list