Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得Python正则表达式的字符类的完整列表以快速查找特定字符?_Python_Regex - Fatal编程技术网

如何获得Python正则表达式的字符类的完整列表以快速查找特定字符?

如何获得Python正则表达式的字符类的完整列表以快速查找特定字符?,python,regex,Python,Regex,此python提供了元字符的完整列表 .^$*+?{}[]\\\\() 类似地,是否有一个页面给出字符类的完整列表 我假设文档中的“字符类”指的是某种特殊字符的有限数,而不是所有可能的unicode字符。如有必要,请纠正我 我做了搜索,但没有找到规范术语 如果“字符类”确实指的是所有可能的unicode字符,我想把我的问题改为“在python中查找正则表达式特殊字符的方便方法” 这似乎是所谓的“速记字符类” (我正在寻找的)更积极的例子是\d,\s,\s,\A等;负面例子(我不想找)是abcde

此python提供了元字符的完整列表

.^$*+?{}[]\\\\()

类似地,是否有一个页面给出字符类的完整列表

我假设文档中的“字符类”指的是某种特殊字符的有限数,而不是所有可能的unicode字符。如有必要,请纠正我

我做了搜索,但没有找到规范术语

如果“字符类”确实指的是所有可能的unicode字符,我想把我的问题改为“在python中查找正则表达式特殊字符的方便方法”

这似乎是所谓的“速记字符类”

(我正在寻找的)更积极的例子是
\d
\s
\s
\A
等;负面例子(我不想找)是
abcdefghijklmnopqrstuvwxyz012456789

我在Python文档和stackoverflow上搜索了“字符类”和“速记字符类”,但没有找到我想要的

为什么我需要这个?当我读到的一部分,如

字符类,如\w或\S(定义如下)也可在集合内接受,尽管它们匹配的字符取决于ASCII或区域设置模式是否有效

我想知道
\w
代表什么。要么在文档中搜索,要么在谷歌上搜索,都需要一些时间。例如,在该文档上使用chrome的搜索菜单命令,
\w
将获得41个结果


如果有这些字符的列表,我可以通过不超过2次的搜索(小写和大写)来查找所有字符。

您是在查找
string.printable
还是
过滤器(lambda x:not x.isalnum(),string.printable)
,返回

!“#$%&\'()*+,-./:;?@[\]^ `{124;}~\ t\n\r\x0b\x0c


您是在寻找返回的
string.printable
还是
过滤器(lambda x:not x.isalnum(),string.printable)

!“#$%&\”()*+,-./:@[\\]^ `{124;}~\ t\n\r\x0b\x0c


看起来您正在寻找Python的
re
模块支持的所有速记字符类。像
[abc]
这样的东西也属于“字符类”的范畴,尽管这在
re
文档中可能并不明显,而且试图列出这些内容的完整列表是不可能的,也是毫无意义的

字符类是用于匹配单个字符的正则表达式语法,通常通过指定它属于或不属于某个字符集来实现。类似于
[abc]
的语法允许您显式指定要匹配的字符集,而类似于
\d
的速记字符类则是大型预定义字符集的速记

Python的
re
模块支持6个速记字符类:
\d
,它匹配数字,
\s
,它匹配空格,
\w
,它匹配“word”字符,
\d
\s
\w
,它们匹配任何字符,和
\w
不匹配。具体哪些字符计数或不计数取决于您使用的是Unicode字符串还是ByTestRing,以及是否设置了
ASCII
LOCALE
标志;有关更多详细信息,请参阅
re
文档(希望对
\w
的模糊文档感到失望)

还有很多其他具有特殊含义的反斜杠字母序列,但它们不是字符类。例如,
\b
匹配单词边界(或者如果忘记使用原始字符串,则在正则表达式引擎看到它之前,它会被解释为退格字符),但这不是字符类


其他正则表达式实现可能支持不同的速记字符类,并且它们的速记字符类可能匹配不同的字符。例如,Perl有更多这样的功能,Perl的
\w
比Python匹配更多的字符,比如组合变音符号。

看起来您正在寻找Python的
re
模块支持的所有速记字符类。像
[abc]
这样的东西也属于“字符类”的范畴,尽管这在
re
文档中可能并不明显,而且试图列出这些内容的完整列表是不可能的,也是毫无意义的

字符类是用于匹配单个字符的正则表达式语法,通常通过指定它属于或不属于某个字符集来实现。类似于
[abc]
的语法允许您显式指定要匹配的字符集,而类似于
\d
的速记字符类则是大型预定义字符集的速记

Python的
re
模块支持6个速记字符类:
\d
,它匹配数字,
\s
,它匹配空格,
\w
,它匹配“word”字符,
\d
\s
\w
,它们匹配任何字符,和
\w
不匹配。具体哪些字符计数或不计数取决于您使用的是Unicode字符串还是ByTestRing,以及是否设置了
ASCII
LOCALE
标志;有关更多详细信息,请参阅
re
文档(希望对
\w
的模糊文档感到失望)

还有很多其他具有特殊含义的反斜杠字母序列,但它们不是字符类。例如,
\b
匹配单词边界(或者如果忘记使用原始字符串,则在正则表达式引擎看到它之前,它会被解释为退格字符),但这不是字符类

其他正则表达式实现可能支持不同的速记字符类,并且它们的速记字符类可能匹配不同的字符。例如
>>> from pprint import pprint
>>> import sre_parse

>>> pprint(sre_parse.CATEGORIES)
{'\\A': (AT, AT_BEGINNING_STRING),
 '\\B': (AT, AT_NON_BOUNDARY),
 '\\D': (IN, [(CATEGORY, CATEGORY_NOT_DIGIT)]),
 '\\S': (IN, [(CATEGORY, CATEGORY_NOT_SPACE)]),
 '\\W': (IN, [(CATEGORY, CATEGORY_NOT_WORD)]),
 '\\Z': (AT, AT_END_STRING),
 '\\b': (AT, AT_BOUNDARY),
 '\\d': (IN, [(CATEGORY, CATEGORY_DIGIT)]),
 '\\s': (IN, [(CATEGORY, CATEGORY_SPACE)]),
 '\\w': (IN, [(CATEGORY, CATEGORY_WORD)])
The special sequences consist of "\\" and a character from the list
below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.
    \number  Matches the contents of the group of the same number.
    \A       Matches only at the start of the string.
    \Z       Matches only at the end of the string.
    \b       Matches the empty string, but only at the start or end of a word.
    \B       Matches the empty string, but not at the start or end of a word.
    \d       Matches any decimal digit; equivalent to the set [0-9] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode digits.
    \D       Matches any non-digit character; equivalent to [^\d].
    \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode whitespace characters.
    \S       Matches any non-whitespace character; equivalent to [^\s].
    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
             in bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the
             range of Unicode alphanumeric characters (letters plus digits
             plus underscore).
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
    \W       Matches the complement of \w.
    \\       Matches a literal backslash.
SPECIAL_CHARS = ".\\[{()*+?^$|"
REPEAT_CHARS = "*+?{"
DIGITS = frozenset("0123456789")
OCTDIGITS = frozenset("01234567")
HEXDIGITS = frozenset("0123456789abcdefABCDEF")
ASCIILETTERS = frozenset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
WHITESPACE = frozenset(" \t\n\r\v\f")

ESCAPES = {
    r"\a": (LITERAL, ord("\a")),
    r"\b": (LITERAL, ord("\b")),
    r"\f": (LITERAL, ord("\f")),
    r"\n": (LITERAL, ord("\n")),
    r"\r": (LITERAL, ord("\r")),
    r"\t": (LITERAL, ord("\t")),
    r"\v": (LITERAL, ord("\v")),
    r"\\": (LITERAL, ord("\\"))
}

FLAGS = {
    # standard flags
    "i": SRE_FLAG_IGNORECASE,
    "L": SRE_FLAG_LOCALE,
    "m": SRE_FLAG_MULTILINE,
    "s": SRE_FLAG_DOTALL,
    "x": SRE_FLAG_VERBOSE,
    # extensions
    "a": SRE_FLAG_ASCII,
    "t": SRE_FLAG_TEMPLATE,
    "u": SRE_FLAG_UNICODE,
}