Is it !-\ (characters from 33=ord('!') to 92=ord('\') and '.' and '&' in a set?
I think my interpretation is incorrect based on my test.
But python reference doesn't say anything wrong with my interpretation. http://docs.python.org/library/re.html
preguntado el 08 de noviembre de 11 a las 16:11
r'[!-\.&]' is just a complicated form of writing
It matches all characters with
33 = ord('!') y
46 = ord('.'), i.e. any of the following:
The escaping backslash before
. is ignored in character classes; it is unnecessary (
. matching all characters in a character class wouldn't make any sense). Since the ampersand
& is already in the character class, it is superfluous as well.
Tests may show that the pattern matches
chr(33) a través de
chr(46), but the pattern is not guaranteed to work that way on all systems. Here's why. Character sets vary from system to system.
This is why the Perl regex documentation specifically recommends “to use only ranges that begin from and end at either alphabetics of equal case (
[A-E]), or digits (
[0-9]). Anything else is unsafe.” (Perl regex is relevant because that's the regex used by Python.)
So, if this pattern is ever run on an EBCDIC based platform, it will match a different set of characters. It is only correct to say that the pattern matches
chr(33) a través de
chr(46) on ASCII based platforms.
It seems that the intention of this regex is to match any character between "!" and "." (notice that the slash is escaping the "." character), which are
! " # $ % & ' ( ) * + , - . (from the Unicode table at http://www.tamasoft.co.jp/en/general-info/unicode.html).
Two comments about the expression:
- Usually, you don't need to escape characters within brackets
(except, maybe, by the
- The ampersand symbol "
&" is already contained in the range defined by "
!-.", so it is redundant.
The backslash escapes the dot and the range will thus be from
.. The regex will match:
& is not necessary since it's included in the range, and escaping a dot is not needed either since it's inside a character class.