my string looks like this:
abcd "efgh [data\]" pqrl 12fgd]
I want to parse till ']' which is not proceeded by a backslash '\\'
Can I do it with strtok_r
? If not than how should I do it?
You could do it with strchr
. Here is how I would try to do it (untested):
p = str;
while ((p = strchr(p, ']')) {
if (p > str && *(p-1) != '\')
/* This is it. */
There is no one shot method to doing this using strtok_r
. Since your delimiter is a single character you can always reconstruct the string you want by stuffing back the delimiter if the last character of a token returned by strtok_r
is '\\'.
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "abcd \"efgh [data\\]\" pqrl 12fgd]";
char *tokens[2] = {0};
char *pToken = str;
unsigned int i = 0;
for( tokens[i] = strtok_r( str, "]", &pToken ); ++i < 2;
tokens[i] = strtok_r( NULL, "]", &pToken ) ) {
}
for( i = 0; i < 2; ++i ) {
printf( "token %d = %s\n", i, tokens[i] );
}
for( i = 0; i < 2; ++i ) {
if( tokens[i][strlen(tokens[i]) - 1] == '\\' ) {
tokens[i][strlen(tokens[i])] = ']';
}
}
printf( "output = %s\n", str );
return 0;
}
This outputs:
token 0 = abcd "efgh [data\
token 1 = " pqrl 12fgd
output = abcd "efgh [data\]" pqrl 12fgd
strtok searches for any single character in the set to be searched for. You could split on ] and then check which ones had a preceding \\ but you can't search for a regex with it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.