regexp questoin

M

micklee74

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...?
thanks
 
B

bruno at modulix

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

If you feed re.compile() something that is not a valid regexp, it will
choke.
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.
 
K

Kent Johnson

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
 
F

Fredrik Lundh

> pat = re.compile(r"%s" %(userinput) ) #userinput is passed from
> command line argument

that r"%s" % (userinput) thing is a pointless operation: the "r" prefix
controls how backslashes in a string literal is parsed; the result is an
ordinary string ("%s" in this case).

in other words,

pat = re.compile(r"%s" % (userinput))

is just a slightly convoluted way to write

pat = re.compile(str(userinput))

and since you know that userinput is a string (at least if you got it
from sys.argv), you might as well write

pat = re.compile(userinput)
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

for what pattern? "[-]" is a valid regular expression (it matches a
single minus), but e.g. "[-" is invalid. to deal with this, catch the
exception:

try:
pat = re.compile(userinput)
except re.error:
print "invalid regular expression:", userinput
sys.exit()

hope this helps!

</F>
 
M

micklee74

Fredrik said:
pat = re.compile(r"%s" %(userinput) ) #userinput is passed from
command line argument

that r"%s" % (userinput) thing is a pointless operation: the "r" prefix
controls how backslashes in a string literal is parsed; the result is an
ordinary string ("%s" in this case).

in other words,

pat = re.compile(r"%s" % (userinput))

is just a slightly convoluted way to write

pat = re.compile(str(userinput))

and since you know that userinput is a string (at least if you got it
from sys.argv), you might as well write

pat = re.compile(userinput)
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

for what pattern? "[-]" is a valid regular expression (it matches a
single minus), but e.g. "[-" is invalid. to deal with this, catch the
exception:

try:
pat = re.compile(userinput)
except re.error:
print "invalid regular expression:", userinput
sys.exit()

hope this helps!

</F>


hi. thanks for all the help.
actually, i am doing an application that allows user to delete files by
entering an input pattern. so if he has files that have special
characters such as [-] or others, then i want to filter them off ....
the solution for re.compile(re.escape(userinput)) might work, i have to
give it a try.
thanks again.
 
F

Fredrik Lundh

actually, i am doing an application that allows user to delete files by
entering an input pattern. so if he has files that have special
characters such as [-] or others, then i want to filter them off ....

I think you will have to define what "pattern" means here. since you're
using the RE module, one would have thought that you want the user to
be able to use RE patterns, but you obviously have something else in mind.
the solution for re.compile(re.escape(userinput)) might work, i have to
give it a try.

re.escape(userinput) creates a RE pattern that patches userinput
exactly. if that's what you want, there's no need to use regular
expressions at all; just use the "in" operator:

if userinput in filename:
do something with file

</F>
 
S

Simon Brunning

actually, i am doing an application that allows user to delete files by
entering an input pattern.

If you are looking to match filenames, you might want to have a look
at the fnmatch and glob modules. The patterns that they support are
less powerful and flexible than regex patters, but much simpler, and
specifically designed with filename matching in mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top