• sscanf通过正则表达式从字符串获取参数


    一行代码抵千言。字符串里面有登录名和密码,用特殊字符分隔。现在需要从这个字符串里面读取数据。

    需要用到sscanf函数,以及通配符。

    当然,这里的通配符并非完美(没有过滤非法字符、没有限制字符个数,等等)

    但是,这个Demo只是是满足我们目前的需求的。当前使用,足以。

    #include <stdio.h>
    int main(void)
    {
    
    /* 
    char buffer[] = "2423&dfgdfg&329234";
    char str1[20], str2[20], str3[20];
    sscanf(buffer, "%[^&]&%[^&]&%s", str1, str2, str3);
    printf("str1:%s 
    ", str1);
    printf("str2:%s 
    ", str2);
    printf("str3:%s 
    ", str3);
    */
    
     
    
    char strKey1[32]={};
    char strValue1[32]={};
    char strKey2[32]={};
    char strValue2[32]={};
    char strKey3[32]={};
    char strValue3[32]={};
    int iValue3=0;
    
    printf("hello world----111----->>>
    ");
    sscanf(
    "username=henry1&password=123456&act=1", "%[^=]=%[^&]&%[^=]=%[^&]&%[^=]=%d", strKey1,strValue1, strKey2, strValue2, strKey3, &iValue3);
    printf(
    "strKey1:%s, strValue1:%s ", strKey1, strValue1); printf("strKey2:%s, strValue2:%s ", strKey2, strValue2); printf("strKey3:%s, strValue3:%d ", strKey3, iValue3); printf("hello world----222----->>> "); return 0; }
  • 相关阅读:
    Linux性能及调优指南(翻译)之Linux内存架构
    dtrace4linux
    perlchina2016 大会
    GO 语言圣经 -在线阅读
    dtrace4linux_Example
    hellogcc -100GDB技巧
    ITGEGE在线教育
    编译系统透视:图解编译原理
    C 高级编程5 IO与文件权限
    C 高级编程4 makefile 与 IO
  • 原文地址:https://www.cnblogs.com/music-liang/p/12927252.html
Copyright © 2020-2023  润新知