教程 Python howto-regex

上传人:jiups****uk12 文档编号:39450692 上传时间:2018-05-15 格式:PDF 页数:18 大小:180.53KB
返回 下载 相关 举报
教程 Python howto-regex_第1页
第1页 / 共18页
教程 Python howto-regex_第2页
第2页 / 共18页
教程 Python howto-regex_第3页
第3页 / 共18页
教程 Python howto-regex_第4页
第4页 / 共18页
教程 Python howto-regex_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《教程 Python howto-regex》由会员分享,可在线阅读,更多相关《教程 Python howto-regex(18页珍藏版)》请在金锄头文库上搜索。

1、Regular Expression HOWTORelease 2.6.5Guido van RossumFred L. Drake, Jr., editorApril 25, 2010Python Software Foundation Email: docspython.orgContents1Introductionii2Simple Patternsii 2.1Matching Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ii 2.2Repeatin

2、g Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .iii3Using Regular Expressionsiv 3.1Compiling Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .iv 3.2The Backslash Plague . . . . . . . . . . . . . . . . . . . . . . .

3、. . . . . . . . . . . . . . . . . .v 3.3Performing Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v 3.4Module-Level Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .vii 3.5Compilation Flags . . . . . . . . . . . . . . .

4、 . . . . . . . . . . . . . . . . . . . . . . . . . . . .vii4More Pattern Powerix 4.1More Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ix 4.2Grouping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .x 4.3Non

5、-capturing and Named Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi 4.4Lookahead Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii5Modifying Stringsxiv 5.1Splitting Strings . . . . . . . . . . . . . . . . . . . . . . . . .

6、 . . . . . . . . . . . . . . . . . . . xiv 5.2Search and Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv6Common Problemsxvi 6.1Use String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi 6.2match() versus

7、search() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi 6.3Greedy versus Non-Greedy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii 6.4Not Using re.VERBOSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8、. . xvii7FeedbackxviiiAuthor A.M. Kuchling Release 0.05AbstractThis document is an introductory tutorial to using regular expressions in Python with the re module. It provides a gentler introduction than the corresponding section in the Library Reference.1 IntroductionThe re module was added in Pyth

9、on 1.5, and provides Perl-style regular expression patterns. Earlier versions of Python came with the regex module, which provided Emacs-style patterns. The regex module was removed completely in Python 2.5.Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highl

10、y specialized program- ming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sen- tences, or e-mail addresses, or TeX commands, or anythin

11、g you like. You can then ask questions such as “Does this string match the pattern?”, or “Is there a match for the pattern anywhere in this string?”. You can also use REs to modify a string or to split it apart in various ways.Regular expression patterns are compiled into a series of bytecodes which

12、 are then executed by a matching engine written in C. For advanced use, it may be necessary to pay careful attention to how the engine will execute a given RE, and write the RE in a certain way in order to produce bytecode that runs faster. Optimization isnt covered in this document, because it requ

13、ires that you have a good understanding of the matching engines internals.The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. There are also tasks that can be done with regular expressions, but the ex-

14、 pressions turn out to be very complicated. In these cases, you may be better off writing Python code to do the processing; while Python code will be slower than an elaborate regular expression, it will also probably be more understandable.2 Simple PatternsWell start by learning about the simplest p

15、ossible regular expressions. Since regular expressions are used to operate on strings, well begin with the most common task: matching characters.For a detailed explanation of the computer science underlying regular expressions (deterministic and non-deterministic finite automata), you can refer to a

16、lmost any textbook on writing compilers.2.1 Matching CharactersMost letters and characters will simply match themselves. For example, the regular expression test will match the string test exactly. (You can enable a case-insensitive mode that would let this RE match Test or TEST as well; more about this later.)Thereareexceptionstothisrule; somecharactersarespecialmetacharacters, anddontmatchthemselves. Instead, they signal that some out-of-the-ordinary thing should be matched, or they affe

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号