Regular expressions with scanf in C

I am trying to achieve the following success:

Delete Opening

message

and trailing

"

leaving content in between and storing it in my variable using sscanf regular expressions. I wrote the following code:

sscanf( buffer, "message \"%[^\"]", message)

Which works when I have something like the message β€œHey there,” but when I try to execute the next line, I get only a space between the two quotes.

message "" "This is a test" ""

The result for this should be "" This is a test ""

, ? google, , . , , , - .

P.S. "" - , .

!

+4
1

, :

:

sscanf(buffer, "message \"%[^$]", message); // remove 'message "'
message[strlen(message) - 1] = '\0'; // remove trailing '"'

, :

char* buffer = ...;
const char* prefix = "message \"";
const char* suffix = "\"";

if (strstr(buffer, prefix) != buffer) {
    // error, doesn't start with `prefix`
}

buffer += strlen(prefix);

char* suffixStart = strrchr(buffer, suffix[0]);
if (!suffixStart || strcmp(suffixStart, suffix) != 0) {
    // error, doesn't end with `suffix`
}

*suffixStart = '\0'; // strip `suffix`
+2

Source: https://habr.com/ru/post/1665229/


All Articles