bruno at modulix
onurb at xiludom.gro
Fri Jun 9 18:19:03 CEST 2006
micklee74 at hotmail.com wrote:
> 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 [-]
> but the script produce some error: sre_constants.error: unexpected end
> of regular expression
If you feed re.compile() something that is not a valid regexp, it will
> how can i successful catch patterns such as "[-]" in my regexp
> compilation where input is unknown...?
If what you want is to validate that the input is a valid regexp, just
try to compile it. In your case (cli argument), there's not much you can
do if the arg is not a valid regexp, except printing a message to stderr
and sys.exit()ing with a non-zero value.
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list