How to match non-printable characters in a python regular expression?

user680696 picture user680696 · Apr 1, 2011 · Viewed 9.5k times · Source

How do you match non-printable characters in a python regular expression? In my case I have a string that has a combination of printable and non-printable characters.

Example String: "Det 3 @ NYY 5 ?7" where the ? is either 0x7f or 0x80.

In the above example I need to match 0x7f or 0x80. How do I specify this in a python regex?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Apr 1, 2011

Use a character range.

'[\x7f\x80]'