使用正则表达式解析以分号分隔的字符串
Parse string separated with semicolon using a regex
我正在尝试使用正则表达式分隔一串数字。当数字用逗号分隔时,以下 C 代码有效:
#include <stdio.h>
int main()
{
char *multiple = "0.20,0.37,0.75,0.56";
char one[4];
char two[4];
char three[4];
char four[4];
sscanf(multiple, "%[^','], %[^','], %[^','], %[^',']", one, two, three, four);
printf("one %s, two %s, three %s, four %s\n", one, two, three, four);
return 0;
}
但是,在我的代码中,它们是用分号分隔的,我想做同样的事情。只是,它在这种情况下不起作用:
#include <stdio.h>
int main()
{
char *multiple = "0.20;0.37;0.75;0.56";
char one[4];
char two[4];
char three[4];
char four[4];
sscanf(multiple, "%[^';'], %[^';'], %[^';'], %[^';']", one, two, three, four);
printf("one %s, two %s, three %s, four %s\n", one, two, three, four);
return 0;
}
谁能告诉我为什么会这样以及如何解决?
做与您的评论相同的事情,但使用分号
sscanf(multiple, "%[^';'];%[^';'];%[^';'];%[^';']", one, two, three, four);
我不知道为什么,因为 scanf 函数族中的格式说明符通常不被认为是一种正则表达式。而且我不知道有关 scanf 功能的所有细节。
在这里,这应该有效:
#include <stdio.h>
int main()
{
char * multiple("0.20,0.37,0.75,0.56");
char one[10];
char two[10];
char three[10];
char four[10];
sscanf(multiple, "%[^';'];%[^';'];%[^';'];%[^';']", one, two, three, four);
printf("one %f, two %f, three %s, four %s\n", one, two, three, four);
return 0;
}
scanf 不支持正则表达式。它支持给定字符集的字符串。当您的格式包含 %[^';']
时,它匹配任何一个或多个字符的序列 除了 '
和 ;
。当您的格式包含逗号 (,
) 时,它匹配逗号。
所以当你说:
sscanf(multiple, "%[^';'], %[^';'], %[^';'], %[^';']", one, two, three, four);
它会匹配除'
和;
以外的所有字符,并将它们存储在one
中。然后它会尝试匹配 ,
,这将失败(导致 scanf 到 return 1 —— 一个匹配和存储的东西)因为任何逗号都将包含在 one
—— 下一个字符只能是 ;
或 '
.
你想要的是
if (sscanf(multiple, "%[^;];%[^;];%[^;];%[^;]", one, two, three, four) != 4)
/* failed -- do something appropriate */
你应该总是检查 scanf 的 return 值,看看它是否匹配你所有的模式,并抓取你认为应该抓取的东西。
还要注意格式中缺少 spaces -- space 将匹配(并跳过)字符串中任何 0 个或多个白色 space 字符的序列。这实际上可能是您想要的(在您提取的每个字段中去除前导白色space),但不是您所描述的
我正在尝试使用正则表达式分隔一串数字。当数字用逗号分隔时,以下 C 代码有效:
#include <stdio.h>
int main()
{
char *multiple = "0.20,0.37,0.75,0.56";
char one[4];
char two[4];
char three[4];
char four[4];
sscanf(multiple, "%[^','], %[^','], %[^','], %[^',']", one, two, three, four);
printf("one %s, two %s, three %s, four %s\n", one, two, three, four);
return 0;
}
但是,在我的代码中,它们是用分号分隔的,我想做同样的事情。只是,它在这种情况下不起作用:
#include <stdio.h>
int main()
{
char *multiple = "0.20;0.37;0.75;0.56";
char one[4];
char two[4];
char three[4];
char four[4];
sscanf(multiple, "%[^';'], %[^';'], %[^';'], %[^';']", one, two, three, four);
printf("one %s, two %s, three %s, four %s\n", one, two, three, four);
return 0;
}
谁能告诉我为什么会这样以及如何解决?
做与您的评论相同的事情,但使用分号
sscanf(multiple, "%[^';'];%[^';'];%[^';'];%[^';']", one, two, three, four);
我不知道为什么,因为 scanf 函数族中的格式说明符通常不被认为是一种正则表达式。而且我不知道有关 scanf 功能的所有细节。
在这里,这应该有效:
#include <stdio.h>
int main()
{
char * multiple("0.20,0.37,0.75,0.56");
char one[10];
char two[10];
char three[10];
char four[10];
sscanf(multiple, "%[^';'];%[^';'];%[^';'];%[^';']", one, two, three, four);
printf("one %f, two %f, three %s, four %s\n", one, two, three, four);
return 0;
}
scanf 不支持正则表达式。它支持给定字符集的字符串。当您的格式包含 %[^';']
时,它匹配任何一个或多个字符的序列 除了 '
和 ;
。当您的格式包含逗号 (,
) 时,它匹配逗号。
所以当你说:
sscanf(multiple, "%[^';'], %[^';'], %[^';'], %[^';']", one, two, three, four);
它会匹配除'
和;
以外的所有字符,并将它们存储在one
中。然后它会尝试匹配 ,
,这将失败(导致 scanf 到 return 1 —— 一个匹配和存储的东西)因为任何逗号都将包含在 one
—— 下一个字符只能是 ;
或 '
.
你想要的是
if (sscanf(multiple, "%[^;];%[^;];%[^;];%[^;]", one, two, three, four) != 4)
/* failed -- do something appropriate */
你应该总是检查 scanf 的 return 值,看看它是否匹配你所有的模式,并抓取你认为应该抓取的东西。
还要注意格式中缺少 spaces -- space 将匹配(并跳过)字符串中任何 0 个或多个白色 space 字符的序列。这实际上可能是您想要的(在您提取的每个字段中去除前导白色space),但不是您所描述的