Re: CODE: Regexp/email check

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 06/07/01


On Thu, 7 Jun 2001, Torgny 'Artovil' Bjers wrote:

> If there is no built in regexp checking, what do I need to be able to make
> a regexp check in Circle?

It depends upon your platform.  glibc includes regular expression matching
functions as a default, see regcomp(3).  An example of their use

  #include <regex.h>

  static regex_t *x_badEmailArray = NULL;
  static uint x_szBadEmailArray = 0;

  void log_regex_error(int err, const regex_t *rx)
  {
    char buf[MAX_STRING_LENGTH];
    regerror(err, rx, buf, MAX_STRING_LENGTH);
    log("regex: %s", buf);
  }

  int read_bad_emails(void)
  {
    char line[MAX_INPUT_LENGTH];
    FILE *fp;
    int err;
    int i;

    if (!(fp = fopen(BAD_EMAIL_FILE, "r"))) {
      perror(BAD_EMAIL_FILE);
      return (-1);
    }

    while (get_line(fp, line))
      x_szBadEmailArray++;

    rewind(fp);
    CREATE(x_badEmailArray, regex_t, x_szBadEmailArray);
    i = 0;

    while (get_line(fp, line) && i < x_szBadEmailArray) {
      err = regcomp(&x_badEmailArray[i], line,
                    REG_EXTENDED | REG_ICASE | REG_NOSUB);

      if (err) {
        log_regex_error(err, &x_badEmailArray[i]);
        continue;
      }
    }

    fclose(fp);
    return (0);
  }

  bool is_bad_email(const char *addr)
  {
    int err;
    int i;

    /* regexec(3) returns 0 on match, REG_NOMATCH on mismatch. */
    for (i = 0; i < x_szBadEmailArray; i++)
      if (!regexec(&x_badEmailArray[i], addr, 0, NULL, 0))
        break;

    return (i < x_szBadEmailArray);
  }

If your platform doesn't have a regular expression library available by
default, you'll have to find one.  I don't know any off the top of my
head, but that's why the gods made Google.  A quick search revealed the
Perl Compatible Regular Expression library, which appears to be portable
and powerful, if a bit overkill for just matching needs:

  http://www.pcre.org/

Point to be made: regular expressions are probably overkill for almost
everything you'd want to do with your Mud.  What are you trying to do,
exactly?  There's probably a considerably more concise way to pull it off
than by using regex.


--
Daniel A. Koepke (dak), dkoepke@circlemud.org
Caveat emptor: I say what I mean and mean what I say.  Listen well.
Caveat venditor: Say what you mean, mean what you say.  Say it well.

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST