7

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?

2 Answers2

13

Use a character range.

'[\x7f\x80]'
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    @Ignacio Since _dehlenbe_ seems to be rarely present on SO, I ask you: **'?'** isn't a non-printable character, but _dehlenbe_ used this character as a symbol in **"Det 3 @ NYY 5 ?7"** to represent a non-printable character that have no symbol consisting in only one character ? Is it that ? – eyquem Aug 24 '11 at 11:14
  • @eyquem: Correct. In globs, the `?` wildcard matches a single character. – Ignacio Vazquez-Abrams Aug 24 '11 at 20:07
1

Maybe you can try

[^[:print:]]

for non-printable.

kurumi
  • 25,121
  • 5
  • 44
  • 52