Using regular expressions to validate data 
Author Message
 Using regular expressions to validate data

A newbie question. Is data validation a task for regular expressions? I
mean for example checking data from a web form before storing or processing
it. It seems to me that only relatively simple tasks can be made without
too much complication. This is a very general question of course, and I
only require a general answer.

In particular, it seems hard to check ranges of numbers, not to mention
ranges of dates, because regular expressions doesn't have the concept of
numbers or dates.

Gustaf



Fri, 30 Dec 2005 20:59:07 GMT  
 Using regular expressions to validate data


% A newbie question. Is data validation a task for regular expressions?

It's not exactly a typical newbie question. REs can be a component of
data validation, but for the reasons you note, they can't do the whole
job. Look at XML Schema for an approach to data validation which uses
REs in part.

One can do something like this

 x !~ /^(yes|no)$/ { invalid() }

but I think it's more typical to use REs for more general validation
and use array look-ups to test enumerations. As you note, range
checks require explicit coding.
  x !~ /[[:alpha:]]+/ { notanident() }
  ! (x in keywords) { notakeyword() }

--

Patrick TJ McPhee
East York  Canada



Sat, 31 Dec 2005 00:02:32 GMT  
 Using regular expressions to validate data

Quote:
> It's not exactly a typical newbie question. REs can be a component of
> data validation, but for the reasons you note, they can't do the whole
> job. Look at XML Schema for an approach to data validation which uses
> REs in part.

Thanks, that was the answer I was looking for. While I'm familiar with the
ideas behind REs, I use them so sparsely that I always feel like a newbie
when I need them. Thanks again.

Gustaf



Sat, 31 Dec 2005 20:56:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. regular expression to extra data from html table?

2. Using variables inside a regular expression

3. AWK: Using variables in regular Expressions

4. AWK: Using variables in regular expressions

5. Using a variable as a Regular Expression

6. a question of using regular expression

7. regular expression using eregi()

8. Using the re module (Regular expressions)

9. pattern matching using regular expressions

10. Help with some basic regular expressions using tcl

11. need help on COMPLEX regular expression problem using Expect

12. Help with regular expression using findall and .*?

 

 
Powered by phpBB® Forum Software