3.2.5.2 模拟C函数 scanf()功能
2015-09-05 09:17
399 查看
在Python里,没有与scanf()直接等同的功能函数,因此需要格式化输入,就需要使用正则表达式的功能来实现,并且正则表达式的功能比scanf()更加灵活,功能更加强大,下面就来列出一些等同的表达:
输入一个字符串的例子:
/usr/sbin/sendmail - 0 errors, 4 warnings
对于上面格式的字符串,如果使用C函数scanf()来输入,需要使用下面的格式来实现:
%s - %d errors, %d warnings
如果我们使用正则表达式来表示,如下:
(\S+) - (\d+) errors, (\d+) warnings
例子:
print('scanf()')
pattern = re.compile(r"(\S+) - (\d+) errors, (\d+) warnings")
match = pattern.match('/usr/sbin/sendmail - 0 errors, 4 warnings')
if match:
print(match.groups())
结果输出如下:
scanf()
('/usr/sbin/sendmail', '0', '4')
%c的例子:
print('scanf() %c')
pattern = re.compile(r".")
match = pattern.match('this is for test\n')
if match:
print(match.group())
结果输出如下:
scanf() %c
t
%5c的例子:
print('scanf() %5c')
pattern = re.compile(r".{5}")
match = pattern.match('this is for test\n')
if match:
print(match.group())
结果输出如下:
scanf() %5c
this
%e, %E, %f, %g的例子:
print('scanf() %e, %E, %f, %g')
pattern = re.compile(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")
match = pattern.match('+200.3721\n')
if match:
print(match.group())
match = pattern.match('x9876\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %e, %E, %f, %g
+200.3721
%i的例子:
print('scanf() %i')
pattern = re.compile(r"[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)")
match = pattern.match('0xAA55\n')
if match:
print(match.group())
match = pattern.match('234.56\n')
if match:
print(match.group())
结果输出如下:
scanf() %i
0xAA55
234
八进制的%o的例子:
print('scanf() %o')
pattern = re.compile(r"[-+]?[0-7]+")
match = pattern.match('0756\n')
if match:
print(match.group())
match = pattern.match('898\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %o
0756
字符串%s的例子:
print('scanf() %s')
pattern = re.compile(r"\S+")
match = pattern.match('深圳是一个小渔村\n')
if match:
print(match.group())
match = pattern.match('898\n')
if match:
print(match.group())
结果输出如下:
scanf() %s
深圳是一个小渔村
898
%u的例子:
print('scanf() %u')
pattern = re.compile(r"\d+")
match = pattern.match('756\n')
if match:
print(match.group())
match = pattern.match('-898\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %u
756
十六进制%x, %X的例子:
print('scanf() %x %X')
pattern = re.compile(r"[-+]?(0[xX])[\dA-Fa-f]+")
match = pattern.match('0x756\n')
if match:
print(match.group())
match = pattern.match('-898\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %x %X
0x756
蔡军生 QQ:9073204 深圳
scanf()格式字符串 | 正则表达式 |
%c | . |
%5c | .{5} |
%d | [-+]?\d+ |
%e,%E,%f,%g | [-+]?(\d+(\.d*)?|\.\d+)([eE][-+]?\d+)? |
%i | [-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+) |
%o | [-+]?[0-7]+ |
%s | \S+ |
%u | \d+ |
%x,%X | [-+]?(0[xX])?[\dA-Fa-f]+ |
/usr/sbin/sendmail - 0 errors, 4 warnings
对于上面格式的字符串,如果使用C函数scanf()来输入,需要使用下面的格式来实现:
%s - %d errors, %d warnings
如果我们使用正则表达式来表示,如下:
(\S+) - (\d+) errors, (\d+) warnings
例子:
print('scanf()')
pattern = re.compile(r"(\S+) - (\d+) errors, (\d+) warnings")
match = pattern.match('/usr/sbin/sendmail - 0 errors, 4 warnings')
if match:
print(match.groups())
结果输出如下:
scanf()
('/usr/sbin/sendmail', '0', '4')
%c的例子:
print('scanf() %c')
pattern = re.compile(r".")
match = pattern.match('this is for test\n')
if match:
print(match.group())
结果输出如下:
scanf() %c
t
%5c的例子:
print('scanf() %5c')
pattern = re.compile(r".{5}")
match = pattern.match('this is for test\n')
if match:
print(match.group())
结果输出如下:
scanf() %5c
this
%e, %E, %f, %g的例子:
print('scanf() %e, %E, %f, %g')
pattern = re.compile(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")
match = pattern.match('+200.3721\n')
if match:
print(match.group())
match = pattern.match('x9876\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %e, %E, %f, %g
+200.3721
%i的例子:
print('scanf() %i')
pattern = re.compile(r"[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)")
match = pattern.match('0xAA55\n')
if match:
print(match.group())
match = pattern.match('234.56\n')
if match:
print(match.group())
结果输出如下:
scanf() %i
0xAA55
234
八进制的%o的例子:
print('scanf() %o')
pattern = re.compile(r"[-+]?[0-7]+")
match = pattern.match('0756\n')
if match:
print(match.group())
match = pattern.match('898\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %o
0756
字符串%s的例子:
print('scanf() %s')
pattern = re.compile(r"\S+")
match = pattern.match('深圳是一个小渔村\n')
if match:
print(match.group())
match = pattern.match('898\n')
if match:
print(match.group())
结果输出如下:
scanf() %s
深圳是一个小渔村
898
%u的例子:
print('scanf() %u')
pattern = re.compile(r"\d+")
match = pattern.match('756\n')
if match:
print(match.group())
match = pattern.match('-898\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %u
756
十六进制%x, %X的例子:
print('scanf() %x %X')
pattern = re.compile(r"[-+]?(0[xX])[\dA-Fa-f]+")
match = pattern.match('0x756\n')
if match:
print(match.group())
match = pattern.match('-898\n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:
scanf() %x %X
0x756
蔡军生 QQ:9073204 深圳
相关文章推荐
- 找到控制面板
- Java中main方法详解
- [Usaco2008 Mar]Cow Travelling游荡的奶牛
- 关于索引的B tree B-tree B+tree B*tree 详解结构图( 二)
- 剑指offer:二叉树的镜像 代码实现
- linux运维实战练习-2015年8月30日课程作业
- hiredis中异步的实现小结
- Android基础View 绘制的流程
- 关于索引的B tree B-tree B+tree B*tree 详解结构图
- 浅谈RAC中的负载均衡
- Linux问题排查:丢失nginx.pid文件
- Sqlite基本命令集合(linux/fedora/ubuntu)
- Sqlite基本命令集合(linux/fedora/ubuntu)
- Java学习之自动装箱和自动拆箱源码分析
- R语言包_Matrix
- Cloudera Hadoop4视频教程|CDH4教程
- Number Sequence(hdu 5014)
- HDU 1269 迷宫城堡 -- 强连通图判断
- Pay Down Your Technical Debt
- Control the Data, Not Just the Code