Start New Discussion within our Software Development Community

Hi

I am trying to validate a URL in-putted by the user to check whether it is syntactically correct.

#include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <regex.h>
    
    printf("Enter the website URL:\n");
    fgets(str, 100, stdin);
    if (!strcmp(str, "\n")) {
        printf("Empty URL ");
        exit(2);
    }

    regex_t regex;
    int reti;
    char msgbuf[100];

    /* Compile regular expression */
    reti = regcomp(&regex, "[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,3}(/[^[:space:]]*)?$", REG_EXTENDED);
    if (reti) {
        fprintf(stderr, "Could not compile regex\n");
        exit(3);
    }

    /* Execute regular expression */
    reti = regexec(&regex, str, 0, NULL, 0);
    if (!reti) {
        puts("Valid URL");
    } else if (reti == REG_NOMATCH) { //This else if always executes.
        puts("Invalid URL");
        exit(4);
    } else {
        regerror(reti, &regex, msgbuf, sizeof (msgbuf));
        fprintf(stderr, "Regex match failed: %s\n", msgbuf);
        exit(5);
    }

    /* Free compiled regular expression if you want to use the regex_t again */
    regfree(&regex);

The problem I am encountering is that the else if on line 24 executes no matter what; even if a valid URL is input like www.google.com

Can anyone see if there is a problem as to why that piece of code is always executing?

Thanks

wouldn't it be a lot easier to just check if the string contains @ and a period? Seems like regex is overkill for this application.

This article has been dead for over six months. Start a new discussion instead.