tags:

views:

324

answers:

4
 [^\x20-\x7E]

I saw this pattern used for a regular expression in which the goal was to remove non-ascii characters from a string. What does it mean?

+1  A: 

It means "anything that isn't a character code in the hexadecimal range 0x20 to 0x7E, i.e. 32 to 126".

chaos
+5  A: 

it says something like: all characters that are not (^) in the range \x20-\x7E (hex 0x20-0x7E). According to http://www.asciitable.com/, those are characters from space to ~.

Flavius Stef
i.e. the printable characters
annakata
+1  A: 

The caret (^) inside the brackets [] means "not", and the \x20-\x7E denotes the byte range of valid ascii characters, where \x20 is the beginning of the range, and \x7E is the end.

I can't say for certain that this is an accurate method for the stated purpose, but that is how the expression is read.

Jay
A: 

It means match any characters that are not printing characters.

Printing characters include a to z, A to Z, 0 to 9 and symbols such as ",;$#% etc.

^ not
\x20 hex code for space character
- to 
\x7e hex code for ~ (tilde) character

All the ascii printing characters fall between these two.

This statement matches non ascii characters as well as ascii control (non printing) characters such as bell, tab, null and others.

Look at

man ascii

on a unix system to see which characters it matches.

In perl, you could also write this as

[^ -~]

or

[[:^cntrl:]]

This last one is slightly different, in that it matches any non control character, including extended ascii (e.g. accented characters) and unicode.

You may not want to restrict yourself to just ascii, since non US locations often use valid printing characters outside this small range, e.g. øüéåç...

Alex Brown
I think you meant [^[:print:]] for that last one. POSIX character class notation includes the square brackets as well as the colons, and the whole thing has to be placed inside another set of square brackets. (And of course, [:cntrl:] is the wrong class.) However, POSIX classes are also supposed to be locale-sensitive, which means they could match, eg, accented letters as well as the basic ASCII set.
Alan Moore
Ah yes, that was sloppy (it was late). cntrl is indeed different to the previous ones, in the sense that it will include printing characters in the extended ascii and even unicode ranges, but I believe that it's likely that's what was intended.
Alex Brown
I would advise leaving the POSIX character classes alone, especially in a case like this, where we don't know which regex flavor is being used, which OS it's running on, or in which locale. All of those factors can change their behavior.
Alan Moore