如何从一串字符中取出 IP 地址
时间:2008-05-09 19:06:50
来源:论坛整理 作者: 编辑:chinaitzhe
请问如何从中得到 IP 地址呢? (C 或是 C 语言)
谢谢各位大侠了!在线等噢!
网友回复:用正则式
网友回复:sscanf使用正规表达的格式串进行扫描试试,没实践过,用其它的正规表达式库应该好实现的
网友回复:正则表达式:
[^\d]\d{3}\.\d{3}\.\d{3}\.\d{3}\[^\d]
网友回复:不知道你想要什么格式的
假如是字符串的话就太简单了
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ string s("2008/05/03,192.168.1.1,test.com"); string strIP(s.substr(11,11));
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <string.h> #include <stdio.h> int main() { char str[]= "2008/05/03,192.168.1.1,test.com"; char *p = strtok(str, ","); p = strtok(NULL, ","); printf("%s", p); return 0; }
网友回复:
- Perl code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ my $str = "2008/05/03,192.168.1.1,test.com"; if($str =~ /,(.*),/) { print ($1); }
网友回复:unsigned long string2ulong(const string& str){
int a, b, c, d;
sscanf(str.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d);
return a (b < < 8) (c < < 16) (d < < 24);
}
string ulong2string(ulong u){
byte * b = (byte *)&u;
char buffer[32];
sprintf(buffer, "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
return buffer;
}
网友回复:假如是需要int型的话
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ string s("2008/05/03,192.168.1.1,test.com"); int a,b,c,d; std::istringstream isstrem(s.substr(11,3)); isstrem >> a; isstrem.clear(); isstrem.str(s.substr(15,3)); isstrem >> b; isstrem.clear(); isstrem.str(s.substr(19,1)); isstrem >> c; isstrem.clear(); isstrem.str(s.substr(21,1)); isstrem >> d;
192 168 1 1 分别被取到int型变量a,b,c,d中!
网友回复:读第一个“,”,开始提取,读到第二个“,”,结束。
char * str = "2008/05/03,192.168.1.1,test.com";
char * des;
char * begin = strchr(str,',');
char * end = strchr(begin 1,',');
int len = end - (begin 1);
des = (char *)malloc(len 1);
strncpy(des,begin 1,len);
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> void getip(char* from, char* to) { while (*from&&*from !=','); while ((*to =*from)&&(*from !=',')); *--to=0; } int main() { char from[]="2008/05/03,192.168.1.1,test.com"; char to[32]={0}; getip(from, to); printf("%s\n", to); return 0; }
网友回复:用sscanf 正则就足够了
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ int main() { int a[4]={0}; sscanf("2008/05/03,192.168.1.1,test.com","%*[^,],%d.%d.%d.%d",a,a 1,a 2,a 3); printf("%d %d %d %d ",a[0],a[1],a[2],a[3]); return 0; }
网友回复:真是非常感谢楼上的各位!!!先试试,得到结果后散分,呵呵!
网友回复:假如是以','分割的话 同意五楼
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> #include <conio.h> int main() { int a[4]={0}; sscanf("2008/05/03,192.168.1.1,test.com","%*[^,],%d.%d.%d.%d",a,a 1,a 2,a 3); printf("%d.%d.%d.%d ",a[0],a[1],a[2],a[3]); getch(); return 0; }
网友回复:正则
网友回复:mark
网友回复:学习中。。。
接分!
网友回复:int main()
{
char p[]= "2008/05/03,192.168.1.1,test.com";
char d[12]={0};
sscanf(p,"2008/05/03,%s,test.com",d);
d[11]=0;
printf("%s\n",d);
}
网友回复:int main()
{
char p[]= "2008/05/03,192.168.1.1,test.com";
int d[4]={0};
sscanf(p,"2008/05/03,%d.%d.%d.%d,test.com",&d[0],&d[1],&d[2],&d[3]);
printf("%d.%d.%d.%d\n",d[0],d[1],d[2],d[3]);
}
网友回复:正则表达式,很好很强大
网友回复:经验之谈啊,不错,多学习……这里宝贵资料挺多
网友回复:正则表达式匹配IP地址的代码:
((2[0-4]\d ¦25[0-5] ¦[01]?\d\d?)\.){3}(2[0-4]\d ¦25[0-5] ¦[01]?\d\d?)
网友回复:mark
网友回复:正则表达式在 C 中怎么用啊?跟 Perl, PHP 的用法一样吗?还请各位大侠再帮帮忙,我要匹配的 IP 地址前后不是只有一个逗号的,例如: 11,04/16/08,08:47:47,Update,192.168.1.1,test.test.com,0015F2060653
我如何才能取出 192.168.1.1 呢?
希望大侠们再次伸出援手啊!!!!
网友回复:[Quote=引用 11 楼 akirya 的回复:]
用sscanf 正则就足够了
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ int main() { int a[4]={0}; sscanf("2008/05/03,192.168.1.1,test.com","%*[^,],%d.%d.%d.%d",a,a 1,a 2,a 3); printf("%d %d %d %d ",a[0],a[1],a[2],a[3]); return 0; }
[/Quote]
网友回复:正则表达式
没用过,很高深的东西啊
网友回复:正则很正点re=/(\d )\.(\d )\.(\d )\.(\d )/g //匹配IP地址的正则表达式
网友回复:#include <string.h>
#include <stdio.h>
int main()
{
char str[]= "2008/05/03,192.168.1.1,test.com";
char *p = strtok(str, ",");
p = strtok(NULL, ",");
printf("%s", p);
return 0;
}
或者
#include <stdio.h>
#include <conio.h>
int main()
{
int a[4]={0};
sscanf("2008/05/03,192.168.1.1,test.com","%*[^,],%d.%d.%d.%d",a,a 1,a 2,a 3);
printf("%d.%d.%d.%d ",a[0],a[1],a[2],a[3]);
getch();
return 0;
}
网友回复:各位所写的都只能是在 IP 地址前面只有一个逗号的情况,可我其实想要的是有很多逗号的情况,就像下面的情况:
11,04/16/08,08:47:47,Update,192.168.1.1,test.test.com,0015F2060653
假如是这样的话我怎么得到 IP 地址呢?
求教,求教!!!!
网友回复:来晚了,能说的上面都说了..
要不楼主自己做个状态机?
网友回复:散分了,借鉴了大家所讲的方法:
1, 先用 strtok 找到 IP 地址之前的逗号
2, 使用 sscanf(sResult, "%d.%d.%d.%d", &a, &b, &c, &d); 取出相应地址
谢谢大家了,学习了!
网友回复:strtok()
用,做token
做两次就把ip地址整个拿出来了
不用sscanf吧。。。
关键字:一串,字符,取出,IP,地址,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面