Regular Expression Howto

Páginas: 34 (8411 palabras) Publicado: 7 de agosto de 2012
Regular Expression HOWTO
Release 0.03

A.M. Kuchling
April 17, 2002
akuchlin@mems-exchange.org Abstract This 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. This document is available in several formats, including PostScript, PDF, HTML and plain ASCII,from the Python HOWTO page at http://www.python.org/doc/howto/.

Contents
1 2 Introduction Simple Patterns 2.1 Matching Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Repeating Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Regular Expressions 3.1 Compiling Regular Expressions 3.2 TheBackslash Plague . . . . . . 3.3 Performing Matches . . . . . . . 3.4 Module-Level Functions . . . . 3.5 Compilation Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 23 4 4 5 5 7 8 9 9 11 12 14 15 15 16 17 17 17 18 19

3

4

More Pattern Power 4.1 More Metacharacters . . . . . . . 4.2 Grouping . . . . . . . . . . . . . 4.3 Non-capturing, and Named Groups 4.4 Other Assertions . . . . . . . . .

5

Modifying Strings 5.1 Splitting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Search and Replace .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Common Problems 6.1 Know About the string Module 6.2 match() versus search() . . . . 6.3 Greedy versus Non-Greedy . . 6.4 Not using re.VERBOSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

7

Feedback

19

1

Introduction

The re module was added in Python 1.5, and provides Perl-style regular expressions. Earlier versions of Python provided the regex module, which provides Emacs-style expressions. Emacs-style is slightly lessreadable, and doesn’t provide as many features, so there’s not much reason to use the regex module when writing new code, though you should be aware of it in order to read older code. Regular expressions (or REs) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for theset of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything 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 arecompiled into a series of bytecodes, which 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 optimize the RE in order to produce bytecode that runs faster. Optimization isn’t really covered in this document, because it requires that you have a good understanding of the matching...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Howto Bte
  • La expressión corporal
  • English Expression
  • Gen expression
  • Reguladores
  • Reguladores
  • regulador
  • Regula

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS