samedi 6 mars 2021

regex matching for whitespaces in a string fails [duplicate]

Suppose cli_content is " abc.txt def.txt" [note the first char is space]

The following snippet would not print error if I am passing 2 filenames. What is wrong with my regular expression?

The execution overlooks both the if statements (with regcomp and reexec) as the condition fails and simply moves on without printing error.

if (cli_content != NULL) {
            
            regex_t the_expression;
            //Use Extended Regular Expressions.
            
if (regcomp(&the_expression, "\\S\\s+\\S", REG_EXTENDED) != 0) {
                error_msg_printer();
                regfree(&the_expression);
                return NULL;
            }

 if (regexec(&the_expression, cli_content, 0, NULL, 0) == 0) {
                error_msg_printer();
                regfree(&the_expression);
                return NULL;
            }

Update: I wrote a function to strip the first and last space character if present, so now I am working with a string that looks like "abc.txt def.txt". how do I setup my regex to look for multiple file names given a sinput?

Aucun commentaire:

Enregistrer un commentaire