Python: POSIX character class in regex?

耗尽温柔 提交于 2019-12-08 16:37:44

问题


How can I search for, say, a sequence of 10 isprint characters in a given string in Python?

With GNU grep, I would simply do grep [[:print:]]{10}


回答1:


Since POSIX is not supported by Python re module, you have to emulate it with the help of character class.

You can use the one from the regular-expressions.info and add a limiting quantifier {10}:

[\x20-\x7E]{10}

See demo

Alternatively, you can use Matthew Barnett regex module that claims to support POSIX character classes (POSIX character classes are supported.).



来源:https://stackoverflow.com/questions/31915346/python-posix-character-class-in-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!