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}
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}
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.).
[`~!@#$%^&*()_=+\[\]{}\\\|;:\"\'<>.,/?]
only matches ASCII punctuation, it has nothing to do with the concept of "printable chars". So, if you were to use a POSIX character class, it would be [[:punct:]]
. To match punctuation in Python, you can use [^\w\s]
, although there are better and more precise patterns. –
Bleeding [[:print]]
class as [[:punct]]
. Appreciate your correction. –
Mccubbin pip install regex
in the console/terminal) and then use import regex
and pattern = regex.compile(r'[[:print:]]{10}')
. –
Bleeding © 2022 - 2024 — McMap. All rights reserved.
[`~!@#$%^&*()_=+\[\]{}\\\|;:\"\'<>.,/?]
when using inside there.sub()
method – Mccubbin