0

I have the following character pointer

char message[100] = "START_MESSAGE hello world \r\n\r\n";

I am trying to use regex.h to parse the above message. I want to get anything between START_MESSAGE and \r\n\r\n.

So, I tried the following code (by following answer of this SO post):

#include <stdio.h>
#include <regex.h>
#include <stdlib.h>

int main() {
    regex_t regex;
    int reti;
    char msgbuf[100];

    reti = regcomp(&regex, "START_MESSAGE*\r\n\r\n", 0);
    reti = regexec(&regex, "START_MESSAGE hello world\r\n\r\n", 0, NULL, 0);

    if (!reti) {
        puts("Match");
    } else
    if (reti == REG_NOMATCH) {
        puts("No match");
    } else {
        regerror(reti, &regex, msgbuf, sizeof(msgbuf));
        fprintf(stderr, "Regex match failed: %s\n", msgbuf);
        exit(1);
    }

    /* Free memory allocated to the pattern buffer by regcomp() */
    regfree(&regex);

    return 0;
}

But, I get no match. I thought, maybe its because of the escape sequence. So, I put \\r\\n\\r\\n and still get no match. I looked for raw string literal (like r before the string in Python). But, I get

error: stray ‘R’ in program

I tried removing \r\n\r\n and looked for only START_MESSAGE pattern, I get a match. How can I get \r\n\r\n to be matched or get the text between START_MESSAGE and \r\n\r\n.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rawwar
  • 4,834
  • 9
  • 32
  • 57
  • 5
    `"START_MESSAGE*"` is searching for `"START_MESSAG"` followed by any number of `E`, I do believe, while it looks like you expect it to search for `"START_MESSAGE"` followed by anything. I believe you want `"START_MESSAGE.*"` as `.` is the wildcard that can match against anything. – Christian Gibbons Sep 17 '19 at 14:13
  • 1
    It seems you're mixing up regular expressions and wildcards – Jabberwocky Sep 17 '19 at 14:19
  • Thanks a lot. i thought, i can mix them. i am wrong – rawwar Sep 17 '19 at 14:19
  • @Jabberwocky is there a way to get the substring? – rawwar Sep 17 '19 at 14:20
  • @ChristianGibbons i tried `"START_MESSAGE(.*)\r\n\r\n"`. Clearly its wrong as i don't even get a match – rawwar Sep 17 '19 at 14:21
  • 1
    You need to double-backslash the parentheses, or probably better specify `REG_EXTENDED`. – tripleee Sep 17 '19 at 16:58

0 Answers0