sscanf高级用法-正则处理IP

#include


int main()
{
    char str[100];
    sscanf("123456fengxinBLOG","%[1-9a-z]",str);
    puts(str);

    return 0;
}

用法5: 
//取仅包含指定字符集的字符串
//只取数字和小写字母

123456fengxin

sscanf("123456AfengxinBLOGa1234","%[1-9a-z]",str);---->123456

 

用法6: 
//取需要的字符串 

#include


int main()
{
    int year,month,day;
    sscanf("2017.12.5","%d.%d.%d",&year,&month,&day);
    printf("year:%d,month:%d,day:%d\n",year,month,day);
    return 0;
}


运行结果:
year:2017,month:12,day:5

 

 

+++++++++++++++++看到这里再看IP-愚蠢的人类对正则一无所知+++++++++++++

#include

void GStringseparation4(const char *sor,unsigned char *arr)
{
    int Arr[4];
    sscanf(sor,"%d.%d.%d.%d",&Arr[0],&Arr[1],&Arr[2],&Arr[3]);
    for(char i=0;i<4;i++)
    	arr[i]=Arr[i];
}

int main()
{
    unsigned char arr[4];
    GStringseparation4("192.168.2.34",arr);
        for(char i=0;i<4;i++)
    	printf("%d-",arr[i] );
    return 0;
}

 

你可能感兴趣的:(sscanf高级用法-正则处理IP)