当前位置: 技术问答>linux和unix
谁比较熟悉C下的正则表达式编程,帮个忙
来源: 互联网 发布时间:2015-07-17
本文导语: 目标字符 目标字符 目标字符 有很多这样的连接(我只列了3个),我想把上面所有的“目标字符”(可能不同),取出来?用C下的正则库。regcomp regexec 请教高手,我应该如何做。 | 我写...
目标字符
目标字符
目标字符
有很多这样的连接(我只列了3个),我想把上面所有的“目标字符”(可能不同),取出来?用C下的正则库。regcomp regexec
请教高手,我应该如何做。
目标字符
目标字符
有很多这样的连接(我只列了3个),我想把上面所有的“目标字符”(可能不同),取出来?用C下的正则库。regcomp regexec
请教高手,我应该如何做。
|
我写的对sscanf();的扩展,你仔细阅读一下,我以前用的分隔符是',';现在给你改成了'>'.
注意在分隔符'>'前后不能有空格,这是此函数的缺点,你有兴趣的话完善一下,互相进步.
void sscanfx(char *buf, char *format, ...)
{
char *bf=0;
char *p=0;
char *pp=0;
char *qq=0;
int len=0;
int jmp=0;
va_list ap;
va_start(ap, format);
len=strlen(buf)+1;
bf=(char *)malloc(len);
memset(bf, 0, len);
memcpy(bf, buf, len);
qq=bf;
do
{
if (*format!='%') pp=strchr(format, '%'), jmp=pp-format, format=pp, bf+=jmp;
if (format==0) break;
if (strncmp(format, "%s", strlen("%s"))==0)
{
p=strchr(bf, '>');
if (p) *p=0;
else break;
sscanf(bf, "%s", va_arg(ap, char*));
}
else if (strncmp(format, "%i", strlen("%i"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%i", va_arg(ap, int*));
}
else if (strncmp(format, "%d", strlen("%d"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%d", va_arg(ap, int*));
}
else if (strncmp(format, "%x", strlen("%x"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%x", va_arg(ap, int*));
}
else if (strncmp(format, "%X", strlen("%X"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%X", va_arg(ap, int*));
}
bf=p+1;
format+=3;
}while(*bf);
if (format)
{
if (strncmp(format, "%s", strlen("%s"))==0) sscanf(bf, "%s", va_arg(ap, char*));
else if (strncmp(format, "%i", strlen("%i"))==0) sscanf(bf, "%i", va_arg(ap, int*));
else if (strncmp(format, "%d", strlen("%d"))==0) sscanf(bf, "%d", va_arg(ap, int*));
else if (strncmp(format, "%x", strlen("%x"))==0) sscanf(bf, "%x", va_arg(ap, int*));
else if (strncmp(format, "%X", strlen("%X"))==0) sscanf(bf, "%X", va_arg(ap, int*));
}
va_end(ap);
free(qq);
}
Example:
int main()
{
char s1[256];
char s2[256];
memset(s1, 0, sizeof(s1));
memset(s1, 0, sizeof(s1));
char *buf="目标字符";
sscanfx(buf, "%s>%s", s1, s2);
}
注意在分隔符'>'前后不能有空格,这是此函数的缺点,你有兴趣的话完善一下,互相进步.
void sscanfx(char *buf, char *format, ...)
{
char *bf=0;
char *p=0;
char *pp=0;
char *qq=0;
int len=0;
int jmp=0;
va_list ap;
va_start(ap, format);
len=strlen(buf)+1;
bf=(char *)malloc(len);
memset(bf, 0, len);
memcpy(bf, buf, len);
qq=bf;
do
{
if (*format!='%') pp=strchr(format, '%'), jmp=pp-format, format=pp, bf+=jmp;
if (format==0) break;
if (strncmp(format, "%s", strlen("%s"))==0)
{
p=strchr(bf, '>');
if (p) *p=0;
else break;
sscanf(bf, "%s", va_arg(ap, char*));
}
else if (strncmp(format, "%i", strlen("%i"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%i", va_arg(ap, int*));
}
else if (strncmp(format, "%d", strlen("%d"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%d", va_arg(ap, int*));
}
else if (strncmp(format, "%x", strlen("%x"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%x", va_arg(ap, int*));
}
else if (strncmp(format, "%X", strlen("%X"))==0)
{
p=strchr(bf, '>');
if (p) *p=' ';
else break;
sscanf(bf, "%X", va_arg(ap, int*));
}
bf=p+1;
format+=3;
}while(*bf);
if (format)
{
if (strncmp(format, "%s", strlen("%s"))==0) sscanf(bf, "%s", va_arg(ap, char*));
else if (strncmp(format, "%i", strlen("%i"))==0) sscanf(bf, "%i", va_arg(ap, int*));
else if (strncmp(format, "%d", strlen("%d"))==0) sscanf(bf, "%d", va_arg(ap, int*));
else if (strncmp(format, "%x", strlen("%x"))==0) sscanf(bf, "%x", va_arg(ap, int*));
else if (strncmp(format, "%X", strlen("%X"))==0) sscanf(bf, "%X", va_arg(ap, int*));
}
va_end(ap);
free(qq);
}
Example:
int main()
{
char s1[256];
char s2[256];
memset(s1, 0, sizeof(s1));
memset(s1, 0, sizeof(s1));
char *buf="目标字符";
sscanfx(buf, "%s>%s", s1, s2);
}
|
while(!feof(infile))
{
fscanf(infile,"%[^]",string[i]);
}
{
fscanf(infile,"%[^]",string[i]);
}
|
用regex:
(.*)
(.*)
|
什么是正则表达式??
|
你好。C/C++支持正则表达式?请好心人提供使用步骤或相关线索!!