3

Is there a library which provides either compiled regular expressions or strings of regular expressions for common patterns such as IP addresses, hostnames, phone numbers, etc that is available in C, C++, C#, or Objective-C?

Ideally one that would allow code similar to the following:

#include <patterns.h>
#include <regex.h>

// return 1 if invalid IP or return 0 if valid IP address
int check_ip(const char * str)
{
    regex_t re;

    if ((regcomp(&re, pattern_ip_address(), REG_EXTENDED))
        return(1);

    if ((regexec(&re, str, 0, NULL, 0)))
    {
        regfree(&re);
        return(1);
    };

    regfree(&re);

    return(0);
}

Alternatively a header which defines a list of patterns such as PATTERN_IP_ADDRESS.

amon
  • 57,091
  • 2
  • 89
  • 149
David M. Syzdek
  • 15,360
  • 6
  • 30
  • 40
  • Do you not want to write your own regexes? – aaronman Jul 28 '13 at 02:09
  • 1
    I don’t think it is reasonable to close this question because “Questions asking us to recommend or find a […] library […] are off-topic for Stack Overflow as they tend to attract opinionated answers […]. Instead, describe the problem […].” The problem is abstraction (totally reasonable), and searching for such a library does not yield useful results. Perl’s [Regexp::Common](http://p3rl.org/Regexp::Common) module would be a perfect fit, but is the wrong language. If anybody knows of such a library for C++ etc., it would be nice to share this knowledge. – amon Jul 28 '13 at 02:09
  • 1
    @aaronman I do write my own regular expressions, but after spending an hour researching the rules of IPv6 address representations and then working out the long and messy regex for IPv6, I really wished I knew of a library of existing expressions which would hopefully be better reviewed and maintained than my one off expressions. – David M. Syzdek Jul 28 '13 at 02:19
  • @DavidM.Syzdek if you limit yourself to c++ it might be hard to find – aaronman Jul 28 '13 at 02:22
  • @amon I fully agree, if anyone else needs convinced why I would like to abstract my code so I do not have to directly write the regex for common patterns, checkout the question asking for a regex for matching IPv6: http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses – David M. Syzdek Jul 28 '13 at 02:24
  • @aaronman I would prefer a C library C, but I can also make use of libraries from Objective-C or C++ as well. – David M. Syzdek Jul 28 '13 at 02:33

0 Answers0