Python:正则表达式中的POSIX字符类? [英] Python: POSIX character class in regex?
问题描述
如何在Python中给定的字符串中搜索10个isprint
字符的序列?
How can I search for, say, a sequence of 10 isprint
characters in a given string in Python?
使用GNU grep,我只需执行grep [[:print:]]{10}
With GNU grep, I would simply do grep [[:print:]]{10}
推荐答案
由于Python re
模块不支持POSIX,因此必须在字符类的帮助下对其进行仿真.
Since POSIX is not supported by Python re
module, you have to emulate it with the help of character class.
您可以使用 regular-expressions.info 中的一个并添加限制量词{10}
:
You can use the one from the regular-expressions.info and add a limiting quantifier {10}
:
[\x20-\x7E]{10}
请参见演示
或者,您可以使用 Matthew Barnett regex模块,该模块声称支持POSIX字符类(支持POSIX字符类.).
Alternatively, you can use Matthew Barnett regex module that claims to support POSIX character classes (POSIX character classes are supported.).
这篇关于Python:正则表达式中的POSIX字符类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!