1、Python正则表达式re模块Python正则基本说明之前讲过关于Python正则的,都是理论的东西,现在讲讲Python正则re模块。导入re模块:import re查看帮助文档:print re._doc_下面就是输出的帮助文档:Support for regular expressions (RE).This module provides regular expression matching operations similar tothose found in Perl. It supports both 8-bit and Unicode strings; boththe pat
2、tern and the strings being processed can contain null bytes andcharacters outside the US ASCII range.Regular expressions can contain both special and ordinary characters.Most ordinary characters, like A, a, or 0, are the simplestregular expressions; they simply match themselves. You canconcatenate o
3、rdinary characters, so last matches the string last.The special characters are: . Matches any character except a newline. Matches the start of the string. $ Matches the end of the string or just before the newline at the end of the string. * Matches 0 or more (greedy) repetitions of the preceding RE
4、. Greedy means that it will match as many repetitions as possible. + Matches 1 or more (greedy) repetitions of the preceding RE. ? Matches 0 or 1 (greedy) of the preceding RE. *?,+?,? Non-greedy versions of the previous three special characters. m,n Matches from m to n repetitions of the preceding R
5、E. m,n? Non-greedy version of the above. Either escapes special characters or signals a special sequence./FROM THIS WEBSITE : Indicates a set of characters. A as the first character indicates a complementing set. | A|B, creates an RE that will match either A or B. (.) Matches the RE inside the paren
6、theses. The contents can be retrieved or matched later in the string. (?iLmsux) Set the I, L, M, S, U, or X flag for the RE (see below). (?:.) Non-grouping version of regular parentheses. (?P.) The substring matched by the group is accessible by name. (?P=name) Matches the text matched earlier by th
7、e group named name. (?#.) A comment; ignored. (?=.) Matches if . matches next, but doesnt consume the string. (?!.) Matches if . doesnt match next. (?=.) Matches if preceded by . (must be fixed length). (? patternw+ re.escape(pattern)w+看,不一样了。具体我真的不懂了。flags I IGNORECASE Perform case-insensitive matc
8、hing. L LOCALE Make w, W, b, B, dependent on the current locale. M MULTILINE matches the beginning of lines (after a newline) as well as the string. $ matches the end of lines (before a newline) as well as the end of the string. S DOTALL . matches any character at all, including the newline. X VERBOSE Ignore whitespace and comments for nicer looking REs. U UNICODE Make w, W, b, B, dependent on the Unicode locale.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1