Input validation in C

Posted on May 6, 2009. Filed under: Programming, Software | Tags: , , |

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(&regc, regex, 0);

 /* Does it match ? */
 return regexec(&regc, "800x500", 0, 0, 0) == 0;
}


Read Full Post | Make a Comment ( 1 so far )

Recently on Brain Dump...

Cricket mostly about batting ?

Posted on December 20, 2008. Filed under: Uncategorized | Tags: , , , |

clrscr in linux!

Posted on December 6, 2008. Filed under: Software | Tags: , , , |

One command called ‘clear’

Posted on December 5, 2008. Filed under: Fedora, Software | Tags: , , |

What’s the point ?

Posted on November 24, 2008. Filed under: Philosophy | Tags: , , , , , , |

End of September’s quota

Posted on September 14, 2008. Filed under: Fedora, Software | Tags: , , , |

Red Hat buys Qumranet

Posted on September 5, 2008. Filed under: KVM, Software, XEN | Tags: , , , , , |

My LZ77 implementation

Posted on September 2, 2008. Filed under: Software | Tags: , , , |

Fun with BSNL Modem and SNMP

Posted on August 16, 2008. Filed under: Software | Tags: , , , , , , |

Google Translate – Very Nice..

Posted on August 11, 2008. Filed under: Software | Tags: |

XEN – Very Popular

Posted on July 8, 2008. Filed under: XEN | Tags: |

  • Feed

  • Ohloh

  • Bookmarks

  • Categories

  • Visitors


  • Hits

    • 21,908 hits

Liked it here?
Why not try sites on the blogroll...