Posted on May 6, 2009. Filed under: Programming, Software | Tags: C, programming, regular expressions |
Just today I found a stable and powerful way to validate inputs in C, using regular expressions. Gnulib provides a header called regex.h and doing man regex.h will help you find more. Here’s how you use it.
First, the regular expression should be ‘compiled’ – not in the usual sense, but it’s converted to a format which increases the speed of pattern matching.
Here’s a simple example, which is used to validate rectangular dimension inputs.
#include <regex .h>
int main()
{
char *regex = "[1-9][0-9]\\{1,\\}x[1-9][0-9]\\{1,\\}";
regex_t regc;
regcomp(®c, regex, 0);
/* Does it match ? */
return regexec(®c, "800x500", 0, 0, 0) == 0;
}
Read Full Post |
Make a Comment
( 1 so far )
Recently on Brain Dump...
Posted on December 20, 2008. Filed under: Uncategorized | Tags: batsmen, batting, bowlers, cricket |
Posted on December 6, 2008. Filed under: Software | Tags: clear, clrscr, linux, terminal |
Posted on December 5, 2008. Filed under: Fedora, Software | Tags: linux, terminal clear, terminal hacks |
Posted on November 24, 2008. Filed under: Philosophy | Tags: Chandrayan, Hunger, india, Nuke, Pakistan, Poverty, Religion |
Posted on September 14, 2008. Filed under: Fedora, Software | Tags: bsnl, Fedora, yum, yum-updatesd |
Posted on September 5, 2008. Filed under: KVM, Software, XEN | Tags: kvm, novell, qumranet, redhat, vdi, XEN |
Posted on August 16, 2008. Filed under: Software | Tags: bsnl, net-snmp, pygtk, python, snmp, snmp statistics, tkmib |
Posted on August 11, 2008. Filed under: Software | Tags: google translate |
Posted on July 8, 2008. Filed under: XEN | Tags: XEN |