Forum OpenACS Development: Validating regular expression

Collapse
Posted by Paul Owen on
Hi,

I want to let users to input regular expressions to search in a data table. If someone input an invalid regexp such as:
abc[1
the site will crash because

select * from table where attribute ~* $value

is not a valid query.
I want to validate the input before processing, I have tried

if {[info complete "regexp $value {}"]} {
  show error page
}

This deals with input such as:
abc[1
However the site still crash with:
abc[]
is there any other TCL command that I can use do to validate a regular expression?

Thanks a lot
Paul

Collapse
Posted by Jeff Lu on
Paul have you tried escaping the [ and ] characters?
So it will look like "abc\[\]"
Collapse
Posted by Jeff Davis on
you could do something like this:
if { [ catch { 
   db_foreach queryname {select * from table where attribute ~* :value} { 
         ...
   }
} errMsg ] } { 
    if {[string match {*Invalid regular expression*} $errMsg]}  {
    ...
    }
}
note that you should use a bind variable (the :value) rather than $value. Also, checking for the error message in the string is a little fragile (and you have to have a version of nspostgres that returns it, and if yours doesn't you should get the one in sourceforge).
Collapse
Posted by Paul Owen on
Thanks very much for these