我有这样的代码:域名解析
#include <stdio.h>
#include <string.h>
int main()
{
char buf[255];
char buf2[255];
char myhost[255] = "subdomain.domain.com";
char *pch;
int token_counter = 0;
memset(buf, 0, 255);
memset(buf2, 0, 255);
pch = strtok(myhost, ".");
while (pch != NULL)
{
pch = strtok(NULL, ".");
if (pch == NULL)
{
memset(buf, 0, 255);
strncpy(buf, buf2, strlen(buf2) - 1);
break;
}
token_counter++;
strcat(buf2, pch);
strcat(buf2, ".");
}
printf("Domain: %s\n", buf);
return 0;
}
这是工作的罚款,如果为myhost被定义为subdomain.domain.com但如果是domain.com它表明“COM”作为最终结果。
如何正确检测它是否是子域或域?也许如果我包含已知的tld列表?
这与正则表达式有什么关系?你只是在一个字符上进行标记 – Mark
我以前用其他编程语言的正则表达式来做这件事,我认为在c –
中有类似的东西,但是这不是你的问题请求 – Mark