sscanf/scanf正则用法

原创 2010年09月02日 14:49:00

%[ ] 的用法: %[ ] 表示要读入一个字符集合 , 如果 [ 后面第一个字符是 ”^” ,则表示反意思。

                     [ ] 内的字符串可以是 1 或更多字符组成。空字符集( %[] )是违反规定的,可

                     导致不可预知的结果。 %[^] 也是违反规定的。
         

%[a-z] 读取在 a-z 之间的字符串,如果不在此之前则停止,如

              char s[]="hello, my friend” ;         // 注意 : , 逗号在不 a-z 之间

              sscanf( s, “%[a-z]”, string ) ; // string=hello

%[^a-z] 读取不在 a-z 之间的字符串,如果碰到 a-z 之间的字符则停止,如

              char s[]="HELLOkitty” ;         // 注意 : , 逗号在不 a-z 之间

              sscanf( s, “%[^a-z]”, string ) ; // string=HELLO

%*[^=]    前面带 * 号表示不保存变量。跳过符合条件的字符串。

              char s[]="notepad=1.0.0.1001" ;

       char szfilename [32] = "" ;

       int i = sscanf( s, "%*[^=]", szfilename ) ; // szfilename=NULL, 因为没保存

int i = sscanf( s, "%*[^=]=%s", szfilename ) ; // szfilename=1.0.0.1001

%40c      读取 40 个字符

              The run-time

library does not automatically append a null terminator

to the string, nor does reading 40 characters

automatically terminate the scanf() function. Because the

library uses buffered input, you must press the ENTER key

to terminate the string scan. If you press the ENTER before

        the scanf() reads 40 characters, it is displayed normally,

        and the library continues to prompt for additional input

        until it reads 40 characters

%[^=]     读取字符串直到碰到 ’=’ 号, ’^’ 后面可以带更多字符 , 如:

              char s[]="notepad=1.0.0.1001" ;

       char szfilename [32] = "" ;

       int i = sscanf( s, "%[^=]", szfilename ) ; // szfilename=notepad     

       如果参数格式是: %[^=:] ,那么也可以从 notepad:1.0.0.1001 读取 notepad

             

使用例子:

       char s[]="notepad=1.0.0.1001" ;

char szname [32] = "" ;

char szver [32] = “” ;

sscanf( s, "%[^=]=%s", szname , szver ) ; // szname=notepad, szver=1.0.0.1001

总结: %[] 有很大的功能,但是并不是很常用到,主要因为:

1 、许多系统的 scanf 函数都有漏洞 . ( 典型的就是 TC 在输入浮点型时有时会出错 ).

2 、用法复杂 , 容易出错 .

3 、编译器作语法分析时会很困难 , 从而影响目标代码的质量和执行效率 .

个人觉得第 3 点最致命,越复杂的功能往往执行效率越低下。而一些简单的字符串分析我们可以自已处理。

%d 跟%i 的区别以及scanf和sscanf的用法

scanf 用%i能够获得8/16进制的值, 比如0011就是9,0x11就是17,当然如果读取到08或者09就会出错了。 scanf简介 scanf函数,与printf函数一样,都被定义在st...
  • xinpo66
  • xinpo66
  • 2013年02月22日 11:55
  • 10387

Linux C语言中sscanf 的详细用法

sscanf() - 从一个字符串中读进与指定格式相符的数据. [cpp] view plaincopyprint? 函数原型:    Int  sscanf( string ...
  • yuesichiu
  • yuesichiu
  • 2014年10月28日 11:50
  • 8268

C语言sscanf函数用法总结(一) 正则表达式

#include #include #include #include using namespace std; void sscanf_test(void) { int ret; ...
  • shihui512
  • shihui512
  • 2013年04月24日 23:46
  • 1595

sscanf/scanf正则用法

%[ ] 的用法: %[ ] 表示要读入一个字符集合 , 如果 [ 后面第一个字符是 ”^” ,则表示反意思。                     [ ] 内的字符串可以是 1 或更多字符组成。空...
  • zhaozidong86
  • zhaozidong86
  • 2011年07月16日 23:02
  • 354

findstr正则用法(转)

dos或批处理下findstr正则用法,会了这个我们就可以用批处理实现文本等搜索替换等 1.findstr . 2.txt 或 Findstr "." 2.txt 从文件2.txt中查找...
  • ChinaHuanyang
  • ChinaHuanyang
  • 2011年10月14日 10:03
  • 341

java正则用法

正则表达式java.util.regex正则的包 + 常用的正则规则 Pattern类和Matcher类。首先使用Pattern.compile(regex)编译正则,返回一个Pattern对象,然后...
  • u013015642
  • u013015642
  • 2016年10月09日 15:54
  • 245

正则用法(不错的)

  • 2013年04月02日 10:39
  • 2KB
  • 下载

gawk的一些正则用法

  • 2011年11月10日 15:28
  • 1KB
  • 下载

java正则用法以及js的正则

java:import java.io.UnsupportedEncodingException;import org.apache.oro.text.regex.MalformedPatternEx...
  • aiwo429001
  • aiwo429001
  • 2011年05月06日 16:13
  • 363

%d 跟%i 的区别以及scanf和sscanf的用法

scanf 用%i能够获得8/16进制的值, 比如0011就是9,0x11就是17,当然如果读取到08或者09就会出错了。 scanf简介 scanf函数,与printf函数一样,都被定义在st...
  • Qsir
  • Qsir
  • 2017年11月03日 09:26
  • 171
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sscanf/scanf正则用法
举报原因:
原因补充:

(最多只允许输入30个字)