zoukankan      html  css  js  c++  java
  • 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; }
  • 相关阅读:
    vue 安装scss
    element-ui 弹出添加拖拽功能
    百度网盘SVIP不限速Mac破解版(亲测可用)
    cookie,localStorage和sessionStorage
    ES6中class的实现原理
    闭包的使用场景
    HTTPS原理以及流程
    DOMContentLoaded和load的区别
    js中的函数防抖与节流
    对vuex的理解
  • 原文地址:https://www.cnblogs.com/music-liang/p/12927252.html
Copyright © 2011-2022 走看看