TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: GREGORIE
from: JAN PANTELTJE
date: 2020-03-20 08:23:00
subject: Re: Regexes and C

On a sunny day (Thu, 19 Mar 2020 13:18:58 -0000 (UTC)) it happened Martin
Gregorie  wrote in :

>I spent more time than I should have yesterday trying to understand
>regcomp(), regexec() and regerror() well enough to validate a string
>containing an e-mail address string to make sure that: its structure is
>correct and neither the username nor the domain contains characters they
>shouldn't.
>
>The upshot was that I couldn't do it because I could not write a regex
>that would detect spaces in the address because apparently regcomp
>doesn't provide any way to anchor a regex to either end of a string, so I
>ended up with a negated regex that detects invalid characters in the
>string and hasn't a clue whether its syntactically correct:
>
>[^.a-zA-Z0-9@_-]
>
>This does the trick, but no thanks to the man pages regex(3), which
>describes the C functions, and regex(7), which describes the regex syntax.
>Both are poorly formatted, hard to read, and seem to have omitted useful
>information, such as the inability of specifying anchor points in strincs
>that DO NOT contain newlines.

My reference for libc related functions is libc.info, reg.. is explained there.
I have libc.info as text file, you can also download it'from my site as one big
text file:
 wget http://panteltje.com/pub/libc.info
use editor search for any function, i thas sometimes examples too.
I could not have written my programs without i!

That said, I have never used reg.. and it seems, for detecting illegal or
allowed chars a bit overkill?

I usually set up a loop for that, for all chars in 'string', maybe like this:

#include 
#include 


int main(int argc, char **argv)
{

/* [^.a-zA-Z0-9@_-] */


if(argc != 2)
        {
        fprintf(stderr, "Usage: ./test62 some_text\n");
        fprintf(stderr, "Dummy, you should enter some text!\n");

        exit(1);
        }

char *ptr;
int ok, c;
ptr = argv[1];
ok = 0;
while(1)
        {
        c = *ptr;

        if(c == 0) break;
        else if(c == '^')   ok = 1;
        else if(c == '.')   ok = 1;
        else if(isalnum(c)) ok = 1;
        else if(c == '@')   ok = 1;
        else if(c == '_')   ok = 1;
        else if(c == '-')   ok = 1;
        else ok = 0;

        if(! ok) break;
        ptr++;
        }

if(! ok)
        {
        fprintf(stderr, "Dummy, you should not use char %c in this field!\n",
c);
        }
else
        {
        fprintf(stderr, "Very good!\n");
        }

exit(0);
} /* end function main */


:-)

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.