6.2. re — Operações com expressões regulares

Código Fonte: Lib/re.py


Este módulo fornece operações de correspondência de expressões regulares semelhantes às encontradas em Perl. O nome do módulo vem da abreviação do termo em inglês regular expressions, RE.

Both patterns and strings to be searched can be Unicode strings as well as 8-bit strings. However, Unicode strings and 8-bit strings cannot be mixed: that is, you cannot match a Unicode string with a byte pattern or vice-versa; similarly, when asking for a substitution, the replacement string must be of the same type as both the pattern and the search string.

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.

A solução é usar a notação de string bruta do Python para padrões de expressão regular; as contrabarras não são tratadas de nenhuma maneira especial em uma string literal com o prefixo 'r'. Portanto, r"\n" é uma string de dois caracteres contendo '\' e 'n', enquanto "\n" é uma string de um caractere contendo um nova linha. Normalmente, os padrões serão expressos em código Python usando esta notação de string bruta.

É importante notar que a maioria das operações de expressão regular estão disponíveis como funções e métodos de nível de módulo em expressões regulares compiladas. As funções são atalhos que não exigem que você compile um objeto regex primeiro, mas perdem alguns parâmetros de ajuste fino.

Ver também

The third-party regex module, which has an API compatible with the standard library re module, but offers additional functionality and a more thorough Unicode support.

6.2.1. Sintaxe de expressão regular

Uma expressão regular (ou ER) especifica um conjunto de strings que corresponde a ela; as funções neste módulo permitem que você verifique se uma determinada string corresponde a uma determinada expressão regular (ou se uma determinada expressão regular corresponde a uma determinada string, o que resulta na mesma coisa).

Regular expressions can be concatenated to form new regular expressions; if A and B are both regular expressions, then AB is also a regular expression. In general, if a string p matches A and another string q matches B, the string pq will match AB. This holds unless A or B contain low precedence operations; boundary conditions between A and B; or have numbered group references. Thus, complex expressions can easily be constructed from simpler primitive expressions like the ones described here. For details of the theory and implementation of regular expressions, consult the Friedl book referenced above, or almost any textbook about compiler construction.

Segue uma breve explicação do formato das expressões regulares. Para mais informações e uma apresentação mais suave, consulte o Expressões Regulares HOWTO.

As expressões regulares podem conter caracteres especiais e comuns. A maioria dos caracteres comuns, como 'A', 'a' ou '0', são as expressões regulares mais simples; eles simplesmente se combinam. Você pode concatenar caracteres comuns, de forma que último corresponda à string 'último'. (No restante desta seção, escreveremos ERs neste estilo especial, geralmente sem aspas, e strings para serem correspondidas 'entre aspas simples'.)

Some characters, like '|' or '(', are special. Special characters either stand for classes of ordinary characters, or affect how the regular expressions around them are interpreted. Regular expression pattern strings may not contain null bytes, but can specify the null byte using a \number notation such as '\x00'.

Qualificadores de repetição (*, +, ?, {m,n} etc) não podem ser aninhados diretamente. Isso evita ambiguidade com o sufixo modificador não guloso ?, E com outros modificadores em outras implementações. Para aplicar uma segunda repetição a uma repetição interna, podem ser usados parênteses. Por exemplo, a expressão (?:a{6})* corresponde a qualquer múltiplo de seis caracteres 'a'.

Os caracteres especiais são:

'.'
(Ponto.) No modo padrão, corresponde a qualquer caractere, exceto uma nova linha. Se o sinalizador DOTALL foi especificado, ele corresponde a qualquer caractere, incluindo uma nova linha.
'^'
(Sinal de circunflexo.) Corresponde ao início da string, e no modo MULTILINE também corresponde imediatamente após cada nova linha.
'$'
Corresponde ao final da string ou logo antes da nova linha no final da string, e no modo MULTILINE também corresponde antes de uma nova linha. foo corresponde a ‘foo’ e ‘foobar’, enquanto a expressão regular foo$ corresponde apenas a ‘foo’. Mais interessante, pesquisar por foo.$ em 'foo1\nfoo2\n' corresponde a ‘foo2’ normalmente, mas ‘foo1’ no modo MULTILINE; procurando por um único $ em 'foo\n' encontrará duas correspondências (vazias): uma logo antes da nova linha e uma no final da string.
'*'
Faz com que a ER resultante corresponda a 0 ou mais repetições da ER anterior, tantas repetições quantas forem possíveis. ab* corresponderá a ‘a’, ‘ab’ ou ‘a’ seguido por qualquer número de ‘b’s.
'+'
Faz com que a ER resultante corresponda a 1 ou mais repetições da ER anterior. ab+ irá corresponder a ‘a’ seguido por qualquer número diferente de zero de ‘b’s; não corresponderá apenas a ‘a’.
'?'
Faz com que a ER resultante corresponda a 0 ou 1 repetição da ER anterior. ab? irá corresponder a ‘a’ ou ‘ab’.
*?, +?, ??
The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is matched against <a> b <c>, it will match the entire string, and not just <a>. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only <a>.
{m}
Especifica que exatamente m cópias da ER anterior devem ser correspondidas; menos correspondências fazem com que toda a ER não seja correspondida. Por exemplo, a{6} irá corresponder exatamente a seis caracteres 'a', mas não a cinco.
{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 'a' characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match aaaab or a thousand 'a' characters followed by a b, but not aaab. The comma may not be omitted or the modifier would be confused with the previously described form.
{m,n}?
Faz com que a ER resultante corresponda de m a n repetições da ER precedente, tentando corresponder o mínimo de poucas repetições possível. Esta é a versão não gananciosa do qualificador anterior. Por exemplo, na string de 6 caracteres 'aaaaaa', a{3,5} irá corresponder a 5 caracteres 'a', enquanto a{3,5}? corresponderá apenas a 3 caracteres.
'\'

Ou escapa caracteres especiais (permitindo que você corresponde a caracteres como '*', '?' e assim por diante), ou sinaliza uma sequência especial; sequências especiais são discutidas abaixo.

Se você não estiver usando uma string raw para expressar o padrão, lembre-se de que o Python também usa a contrabarra como uma sequência de escape em literais de string; se a sequência de escape não for reconhecida pelo analisador sintático do Python, a contrabarra e o caractere subsequente serão incluídos na string resultante. No entanto, se o Python reconhecer a sequência resultante, a contrabarra deve ser repetida duas vezes. Isso é complicado e difícil de entender, portanto, é altamente recomendável que você use strings raw para todas as expressões, exceto as mais simples.

[]

Usado para indicar um conjunto de caracteres. Em um conjunto:

  • Caracteres podem ser listados individualmente, por exemplo, [amk] vai corresponder a 'a', 'm' ou 'k'.
  • Ranges of characters can be indicated by giving two characters and separating them by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59, and [0-9A-Fa-f] will match any hexadecimal digit. If - is escaped (e.g. [a\-z]) or if it’s placed as the first or last character (e.g. [a-]), it will match a literal '-'.
  • Os caracteres especiais perdem seu significado especial dentro dos conjuntos. Por exemplo, [(+*)] corresponderá a qualquer um dos caracteres literais '(', '+', '*' ou ')'.
  • Classes de caracteres como \w ou \S (definidas abaixo) também são aceitas dentro de um conjunto, embora os caracteres que correspondem dependam do modo ASCII ou LOCALE está em vigor.
  • Os caracteres que não estão dentro de um intervalo podem ser correspondidos complementando o conjunto. Se o primeiro caractere do conjunto for '^', todos os caracteres que não estiverem no conjunto serão correspondidos. Por exemplo, [^5] irá corresponder a qualquer caractere exceto '5', e [^^] irá corresponder a qualquer caractere exceto '^'. ^ não tem nenhum significado especial se não for o primeiro caractere do conjunto.
  • Para corresponder a um ']' literal dentro de um conjunto, preceda-o com uma contrabarra ou coloque-o no início do conjunto. Por exemplo, [()[\]{}] e []()[{}] ambos corresponderão a um parêntese.
'|'
A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the '|' in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by '|' are tried from left to right. When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the '|' operator is never greedy. To match a literal '|', use \|, or enclose it inside a character class, as in [|].
(...)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use \( or \), or enclose them inside a character class: [(] [)].
(?...)
Esta é uma notação de extensão (um '?' seguindo um '(' não é significativo de outra forma). O primeiro caractere após o '?' determina qual o significado e sintaxe posterior do é. As extensões normalmente não criam um novo grupo; (?P<name>...) é a única exceção a esta regra. A seguir estão as extensões atualmente suportadas.
(?aiLmsux)

(One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the entire regular expression. (The flags are described in Conteúdo do Módulo.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.compile() function.

Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.

(?:...)
Uma versão sem captura de parênteses regulares. Corresponde a qualquer expressão regular que esteja entre parênteses, mas a substring correspondida pelo grupo não pode ser recuperada após realizar uma correspondência ou referenciada posteriormente no padrão.
(?P<name>...)

Semelhante aos parênteses regulares, mas a substring correspondida pelo grupo é acessível por meio do nome de grupo simbólico name. Os nomes de grupo devem ser identificadores Python válidos e cada nome de grupo deve ser definido apenas uma vez em uma expressão regular. Um grupo simbólico também é um grupo numerado, como se o grupo não tivesse um nome.

Grupos nomeados podem ser referenciados em três contextos. Se o padrão for (?P<quote>['"]).*?(?P=quote) (ou seja, corresponder a uma string entre aspas simples ou duplas):

Contexto de referência ao grupo “quote” Formas de referenciá-lo
no mesmo padrão
  • (?P=quote) (como mostrado)
  • \1
when processing match object m
  • m.group('quote')
  • m.end('quote') (etc.)
in a string passed to the repl argument of re.sub()
  • \g<quote>
  • \g<1>
  • \1
(?P=name)
Uma referência anterior a um grupo nomeado; corresponde a qualquer texto que corresponda ao grupo anterior denominado name.
(?#...)
Um comentário; o conteúdo dos parênteses é simplesmente ignorado.
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
(?!...)
Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.
(?<=...)

Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that abc or a|b are allowed, but a* and a{3,4} are not. Note that patterns which start with positive lookbehind assertions will not match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:

>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'

Este exemplo procura por uma palavra logo após um hífen:

>>> m = re.search('(?<=-)\w+', 'spam-egg')
>>> m.group(0)
'egg'

Alterado na versão 3.5: Adicionado suporte para referências de grupo de comprimento fixo.

(?<!...)
Corresponde se a posição atual na string não for precedida por uma correspondência para .... Isso é chamado de asserção retrovisora negativa. Semelhante às asserções retrovisoras positivas, o padrão contido deve corresponder apenas a strings de algum comprimento fixo. Os padrões que começam com asserções retroativas negativas podem corresponder ao início da string que está sendo pesquisada.
(?(id/name)yes-pattern|no-pattern)
Tentará corresponder com padrão-sim se o grupo com determinado id ou nome existir, e com padrão-não se não existir. padrão-não é opcional e pode ser omitido. Por exemplo, (<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$) é um padrão ruim de correspondência de e-mail, que corresponderá com '<usuario@host.com>' bem como 'usuario@host.com', mas não com '<usuario@host.com>' nem 'usuario@host.com>'.

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. For example, \$ matches the character '$'.

\number
Corresponde ao conteúdo do grupo de mesmo número. Os grupos são numerados a partir de 1. Por exemplo, (.+) \1 corresponde a 'de de' ou '55 55', mas não 'dede' (note o espaço após o grupo). Esta sequência especial só pode ser usada para corresponder a um dos primeiros 99 grupos. Se o primeiro dígito de número for 0, ou número tiver 3 dígitos octais de comprimento, ele não será interpretado como uma correspondência de grupo, mas como o caractere com número de valor octal. Dentro de '[' e ']' de uma classe de caracteres, todos os escapes numéricos são tratados como caracteres.
\A
Corresponde apenas ao início da string.
\b

Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of Unicode alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore Unicode character. Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.

By default Unicode alphanumerics are the ones used, but this can be changed by using the ASCII flag. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.

\B
Matches the empty string, but only when it is not at the beginning or end of a word. This means that r'py\B' matches 'python', 'py3', 'py2', but not 'py', 'py.', or 'py!'. \B is just the opposite of \b, so word characters are Unicode alphanumerics or the underscore, although this can be changed by using the ASCII flag.
\d
Para padrões (str) Unicode:
Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [0-9] may be a better choice).
Para padrões de 8 bits (isto é, bytes):
Corresponde a qualquer dígito decimal; isso é equivalente a [0-9].
\D
Matches any character which is not a Unicode decimal digit. This is the opposite of \d. If the ASCII flag is used this becomes the equivalent of [^0-9] (but the flag affects the entire regular expression, so in such cases using an explicit [^0-9] may be a better choice).
\s
Para padrões (str) Unicode:
Matches Unicode whitespace characters (which includes [ \t\n\r\f\v], and also many other characters, for example the non-breaking spaces mandated by typography rules in many languages). If the ASCII flag is used, only [ \t\n\r\f\v] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [ \t\n\r\f\v] may be a better choice).
Para padrões de 8 bits (isto é, bytes):
Corresponde a caracteres considerados espaços em branco no conjunto de caracteres ASCII; isso é equivalente a [ \t\n\r\f\v].
\S
Matches any character which is not a Unicode whitespace character. This is the opposite of \s. If the ASCII flag is used this becomes the equivalent of [^ \t\n\r\f\v] (but the flag affects the entire regular expression, so in such cases using an explicit [^ \t\n\r\f\v] may be a better choice).
\w
Para padrões (str) Unicode:
Matches Unicode word characters; this includes most characters that can be part of a word in any language, as well as numbers and the underscore. If the ASCII flag is used, only [a-zA-Z0-9_] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [a-zA-Z0-9_] may be a better choice).
Para padrões de 8 bits (isto é, bytes):
Matches characters considered alphanumeric in the ASCII character set; this is equivalent to [a-zA-Z0-9_].
\W
Matches any character which is not a Unicode word character. This is the opposite of \w. If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_] (but the flag affects the entire regular expression, so in such cases using an explicit [^a-zA-Z0-9_] may be a better choice).
\Z
Corresponde apenas ao final da string.

A maioria dos escapes padrão suportados por literais de string Python também são aceitos pelo analisador sintático de expressão regular:

\a      \b      \f      \n
\r      \t      \u      \U
\v      \x      \\

(Observe que \b é usado para representar limites de palavras e significa fazer “backspace” apenas dentro das classes de caracteres.)

'\u' and '\U' escape sequences are only recognized in Unicode patterns. In bytes patterns they are not treated specially.

Os escapes octais são incluídos em um formulário limitado. Se o primeiro dígito for 0, ou se houver três dígitos octais, é considerado um escape octal. Caso contrário, é uma referência de grupo. Quanto aos literais de string, os escapes octais têm sempre no máximo três dígitos.

Alterado na versão 3.3: As sequências de escape '\u' e '\U' foram adicionadas.

Deprecated since version 3.5, will be removed in version 3.6: Unknown escapes consisting of '\' and ASCII letter now raise a deprecation warning and will be forbidden in Python 3.6.

Ver também

Mastering Regular Expressions
Book on regular expressions by Jeffrey Friedl, published by O’Reilly. The second edition of the book no longer covers Python at all, but the first edition covered writing good regular expression patterns in great detail.

6.2.2. Conteúdo do Módulo

O módulo define várias funções, constantes e uma exceção. Algumas das funções são versões simplificadas dos métodos completos para expressões regulares compiladas. A maioria dos aplicativos não triviais sempre usa a forma compilada.

re.compile(pattern, flags=0)

Compile a regular expression pattern into a regular expression object, which can be used for matching using its match() and search() methods, described below.

O comportamento da expressão pode ser modificado especificando um valor flags. Os valores podem ser qualquer uma das seguintes variáveis, correspondidas usando OU bit a bit (o operador |).

A sequência

prog = re.compile(pattern)
result = prog.match(string)

é equivalente a:

result = re.match(pattern, string)

mas usar re.compile() e salvar o objeto de expressão regular resultante para reutilização é mais eficiente quando a expressão será usada várias vezes em um único programa.

Nota

As versões compiladas dos padrões mais recentes passados para re.compile() e as funções de correspondência em nível de módulo são armazenadas em cache, de modo que programas que usam apenas algumas expressões regulares por vez não precisam se preocupar em compilar expressões regulares.

re.A
re.ASCII

Make \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching. This is only meaningful for Unicode patterns, and is ignored for byte patterns.

Observe que, para compatibilidade com versões anteriores, o sinalizador re.U ainda existe (bem como seu sinônimo re.UNICODE e sua contraparte incorporada (?u)), mas estes são redundantes em Python 3, pois as correspondências são Unicode por padrão para strings (e a correspondência Unicode não é permitida para bytes).

re.DEBUG

Display debug information about compiled expression.

re.I
re.IGNORECASE

Perform case-insensitive matching; expressions like [A-Z] will match lowercase letters, too. This is not affected by the current locale and works for Unicode characters as expected.

re.L
re.LOCALE

Make \w, \W, \b, \B, \s and \S dependent on the current locale. The use of this flag is discouraged as the locale mechanism is very unreliable, and it only handles one “culture” at a time anyway; you should use Unicode matching instead, which is the default in Python 3 for Unicode (str) patterns. This flag makes sense only with bytes patterns.

Deprecated since version 3.5, will be removed in version 3.6: Deprecated the use of re.LOCALE with string patterns or re.ASCII.

re.M
re.MULTILINE

When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). By default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.

re.S
re.DOTALL

Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.

re.X
re.VERBOSE

This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class or when preceded by an unescaped backslash. When a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.

Isso significa que os dois seguintes objetos de expressão regular que correspondem a um número decimal são funcionalmente iguais:

a = re.compile(r"""\d +  # the integral part
                   \.    # the decimal point
                   \d *  # some fractional digits""", re.X)
b = re.compile(r"\d+\.\d*")
re.search(pattern, string, flags=0)

Percorre a string procurando o primeiro local onde o padrão pattern de expressão regular produz uma correspondência e retorna um objeto de correspondência encontrado. Retorne None se nenhuma posição na string corresponder ao padrão; observe que isso é diferente de encontrar uma correspondência de comprimento zero em algum ponto da string.

re.match(pattern, string, flags=0)

Se zero ou mais caracteres no início da string corresponderem ao padrão pattern da expressão regular, retorna um objeto de correspondência encontrado. Retorna None se a string não corresponder ao padrão; observe que isso é diferente de uma correspondência de comprimento zero.

Observe que mesmo no modo MULTILINE, re.match() irá corresponder apenas no início da string e não no início de cada linha.

Se você quiser localizar uma correspondência em qualquer lugar em string, use search() (veja também search() vs. match()).

re.fullmatch(pattern, string, flags=0)

Se toda a string corresponder ao padrão pattern da expressão regular, retorna um objeto de correspondência encontrado. Retorna None se a string não corresponder ao padrão; observe que isso é diferente de uma correspondência de comprimento zero.

Novo na versão 3.4.

re.split(pattern, string, maxsplit=0, flags=0)

Divide a string pelas ocorrências do padrão pattern. Se parênteses de captura forem usados em pattern, o texto de todos os grupos no padrão também será retornado como parte da lista resultante. Se maxsplit for diferente de zero, no máximo maxsplit divisões ocorrerão e o restante da string será retornado como o elemento final da lista.

>>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
['0', '3', '9']

If there are capturing groups in the separator and it matches at the start of the string, the result will start with an empty string. The same holds for the end of the string:

>>> re.split('(\W+)', '...words, words...')
['', '...', 'words', ', ', 'words', '...', '']

Dessa forma, os componentes do separador são sempre encontrados nos mesmos índices relativos na lista de resultados.

Nota

split() doesn’t currently split a string on an empty pattern match. For example:

>>> re.split('x*', 'axbc')
['a', 'bc']

Even though 'x*' also matches 0 ‘x’ before ‘a’, between ‘b’ and ‘c’, and after ‘c’, currently these matches are ignored. The correct behavior (i.e. splitting on empty matches too and returning ['', 'a', 'b', 'c', '']) will be implemented in future versions of Python, but since this is a backward incompatible change, a FutureWarning will be raised in the meanwhile.

Patterns that can only match empty strings currently never split the string. Since this doesn’t match the expected behavior, a ValueError will be raised starting from Python 3.5:

>>> re.split("^$", "foo\n\nbar\n", flags=re.M)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
ValueError: split() requires a non-empty pattern match.

Alterado na versão 3.1: Adicionado o argumento de sinalizadores opcionais.

Alterado na versão 3.5: Splitting on a pattern that could match an empty string now raises a warning. Patterns that can only match empty strings are now rejected.

re.findall(pattern, string, flags=0)

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

re.finditer(pattern, string, flags=0)

Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match.

re.sub(pattern, repl, string, count=0, flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a carriage return, and so forth. Unknown escapes such as \& are left alone. Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern. For example:

>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
...        r'static PyObject*\npy_\1(void)\n{',
...        'def myfunc():')
'static PyObject*\npy_myfunc(void)\n{'

If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. For example:

>>> def dashrepl(matchobj):
...     if matchobj.group(0) == '-': return ' '
...     else: return '-'
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
'Baked Beans & Spam'

The pattern may be a string or an RE object.

The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match, so sub('x*', '-', 'abc') returns '-a-b-c-'.

Em argumentos repl do tipo string, além dos escapes de caractere e retrovisores descritos acima, \g<nome> usará a substring correspondida pelo grupo denominado nome, conforme definido pela sintaxe (?P<nome>...). \g<número> usa o número do grupo correspondente; \g<2> é portanto equivalente a \2, mas não é ambíguo em uma substituição como \g<2>0. \20 seria interpretado como uma referência ao grupo 20, não uma referência ao grupo 2 seguida pelo caractere literal '0'. O retrovisor \g6 substitui em toda a substring correspondida pela ER.

Alterado na versão 3.1: Adicionado o argumento de sinalizadores opcionais.

Alterado na versão 3.5: Grupos sem correspondência são substituídos por uma string vazia.

Deprecated since version 3.5, will be removed in version 3.6: Unknown escapes consist of '\' and ASCII letter now raise a deprecation warning and will be forbidden in Python 3.6.

re.subn(pattern, repl, string, count=0, flags=0)

Executa a mesma operação como sub(), mas retorna uma tupla (new_string, number_of_subs_made).

Alterado na versão 3.1: Adicionado o argumento de sinalizadores opcionais.

Alterado na versão 3.5: Grupos sem correspondência são substituídos por uma string vazia.

re.escape(pattern)

Escape all the characters in pattern except ASCII letters, numbers and '_'. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. For example:

>>> print(re.escape('python.exe'))
python\.exe

>>> legal_chars = string.ascii_lowercase + string.digits + "!#$%&'*+-.^_`|~:"
>>> print('[%s]+' % re.escape(legal_chars))
[abcdefghijklmnopqrstuvwxyz0123456789\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\:]+

>>> operators = ['+', '-', '*', '/', '**']
>>> print('|'.join(map(re.escape, sorted(operators, reverse=True))))
\/|\-|\+|\*\*|\*

Alterado na versão 3.3: O caractere '_' não é mais escapada.

re.purge()

Limpa o cache de expressão regular.

exception re.error(msg, pattern=None, pos=None)

Exceção levantada quando uma string passada para uma das funções aqui não é uma expressão regular válida (por exemplo, ela pode conter parênteses não correspondentes) ou quando algum outro erro ocorre durante a compilação ou correspondência. Nunca é um erro se uma string não contém correspondência para um padrão. A instância de erro possui os seguintes atributos adicionais:

msg

A mensagem de erro não formatada.

pattern

O padrão da expressão regular.

pos

O índice no padrão pattern no qual a compilação falhou (pode ser None).

lineno

A linha correspondente a pos (pode ser None).

colno

A coluna correspondente a pos (pode ser None).

Alterado na versão 3.5: Adicionados os atributos adicionais.

6.2.3. Objetos expressão regular

Objetos expressão regular compilados oferecem suporte aos seguintes métodos e atributos:

regex.search(string[, pos[, endpos]])

Percorre a string procurando o primeiro local onde esta expressão regular produz uma correspondência e retorna um objeto correspondência encontrado. Retorna None se nenhuma posição na string corresponder ao padrão; observe que isso é diferente de encontrar uma correspondência de comprimento zero em algum ponto da string.

O segundo parâmetro opcional pos fornece um índice na string onde a pesquisa deve começar; o padrão é 0. Isso não é totalmente equivalente a fatiar a string; o caractere padrão '^' corresponde no início real da string e nas posições logo após uma nova linha, mas não necessariamente no índice onde a pesquisa deve começar.

The optional parameter endpos limits how far the string will be searched; it will be as if the string is endpos characters long, so only the characters from pos to endpos - 1 will be searched for a match. If endpos is less than pos, no match will be found; otherwise, if rx is a compiled regular expression object, rx.search(string, 0, 50) is equivalent to rx.search(string[:50], 0).

>>> pattern = re.compile("d")
>>> pattern.search("dog")     # Match at index 0
<_sre.SRE_Match object; span=(0, 1), match='d'>
>>> pattern.search("dog", 1)  # No match; search doesn't include the "d"
regex.match(string[, pos[, endpos]])

Se zero ou mais caracteres no start da string corresponderem a esta expressão regular, retorna um objeto correspondência encontrado. Retorna None se a string não corresponder ao padrão; observe que isso é diferente de uma correspondência de comprimento zero.

The optional pos and endpos parameters have the same meaning as for the search() method.

>>> pattern = re.compile("o")
>>> pattern.match("dog")      # No match as "o" is not at the start of "dog".
>>> pattern.match("dog", 1)   # Match as "o" is the 2nd character of "dog".
<_sre.SRE_Match object; span=(1, 2), match='o'>

If you want to locate a match anywhere in string, use search() instead (see also search() vs. match()).

regex.fullmatch(string[, pos[, endpos]])

Se toda a string corresponder a esta expressão regular, retorna um objeto correspondência encontrado. Retorna None se a string não corresponder ao padrão; observe que isso é diferente de uma correspondência de comprimento zero.

The optional pos and endpos parameters have the same meaning as for the search() method.

>>> pattern = re.compile("o[gh]")
>>> pattern.fullmatch("dog")      # No match as "o" is not at the start of "dog".
>>> pattern.fullmatch("ogre")     # No match as not the full string matches.
>>> pattern.fullmatch("doggie", 1, 3)   # Matches within given limits.
<_sre.SRE_Match object; span=(1, 3), match='og'>

Novo na versão 3.4.

regex.split(string, maxsplit=0)

Idêntico à função split(), usando o padrão compilado.

regex.findall(string[, pos[, endpos]])

Similar to the findall() function, using the compiled pattern, but also accepts optional pos and endpos parameters that limit the search region like for match().

regex.finditer(string[, pos[, endpos]])

Similar to the finditer() function, using the compiled pattern, but also accepts optional pos and endpos parameters that limit the search region like for match().

regex.sub(repl, string, count=0)

Idêntico à função sub(), usando o padrão compilado.

regex.subn(repl, string, count=0)

Idêntico à função subn(), usando o padrão compilado.

regex.flags

Os sinalizadores de correspondência de regex. Esta é uma combinação dos sinalizadores fornecidos para compile(), qualquer sinalizador em linha (?...) no padrão e sinalizadores implícitos como UNICODE se o padrão for uma string Unicode.

regex.groups

O número de grupos de captura no padrão.

regex.groupindex

Um dicionário que mapeia qualquer nome de grupo simbólico definido por (?P<id>) para números de grupo. O dicionário estará vazio se nenhum grupo simbólico for usado no padrão.

regex.pattern

The pattern string from which the RE object was compiled.

6.2.4. Objetos correspondência

Match objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement:

match = re.search(pattern, string)
if match:
    process(match)

Os objetos correspondência oferecem suporte aos seguintes métodos e atributos:

match.expand(template)

Return the string obtained by doing backslash substitution on the template string template, as done by the sub() method. Escapes such as \n are converted to the appropriate characters, and numeric backreferences (\1, \2) and named backreferences (\g<1>, \g<name>) are replaced by the contents of the corresponding group.

Alterado na versão 3.5: Grupos sem correspondência são substituídos por uma string vazia.

match.group([group1, ...])

Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the corresponding parenthesized group. If a group number is negative or larger than the number of groups defined in the pattern, an IndexError exception is raised. If a group is contained in a part of the pattern that did not match, the corresponding result is None. If a group is contained in a part of the pattern that matched multiple times, the last match is returned.

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # The entire match
'Isaac Newton'
>>> m.group(1)       # The first parenthesized subgroup.
'Isaac'
>>> m.group(2)       # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2)    # Multiple arguments give us a tuple.
('Isaac', 'Newton')

Se a expressão regular usa a sintaxe (?P<name>...), os argumentos groupN também podem ser strings que identificam grupos por seus nomes de grupo. Se um argumento string não for usado como um nome de grupo no padrão, uma exceção IndexError é levantada.

A moderately complicated example:

>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
>>> m.group('first_name')
'Malcolm'
>>> m.group('last_name')
'Reynolds'

Named groups can also be referred to by their index:

>>> m.group(1)
'Malcolm'
>>> m.group(2)
'Reynolds'

If a group matches multiple times, only the last match is accessible:

>>> m = re.match(r"(..)+", "a1b2c3")  # Matches 3 times.
>>> m.group(1)                        # Returns only the last match.
'c3'
match.groups(default=None)

Retorna uma tupla contendo todos os subgrupos da correspondência, de 1 até quantos grupos estiverem no padrão. O argumento default é usado para grupos que não participaram da correspondência; o padrão é None.

For example:

>>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
>>> m.groups()
('24', '1632')

If we make the decimal place and everything after it optional, not all groups might participate in the match. These groups will default to None unless the default argument is given:

>>> m = re.match(r"(\d+)\.?(\d+)?", "24")
>>> m.groups()      # Second group defaults to None.
('24', None)
>>> m.groups('0')   # Now, the second group defaults to '0'.
('24', '0')
match.groupdict(default=None)

Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name. The default argument is used for groups that did not participate in the match; it defaults to None. For example:

>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
>>> m.groupdict()
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}
match.start([group])
match.end([group])

Retorna os índices de início e fim da substring correspondidos pelo grupo group; group tem como padrão zero (o que significa que toda a substring é correspondida). Retorna -1 se group existe, mas não contribuiu para a correspondência. Para um objeto correspondência m e um grupo g que contribuiu para a correspondência, a substring correspondida pelo grupo g (equivalente a m.group(g)) é

m.string[m.start(g):m.end(g)]

Observe que m.start(group) será igual a m.end(group) se group correspondeu a uma string nula. Por exemplo, após m = re.search('b(c?)', 'cba'), m.start(0) é 1, m.end(0) é 2, m.start(1) e m.end(1) são 2, e m.start(2) levanta uma exceção IndexError.

An example that will remove remove_this from email addresses:

>>> email = "tony@tiremove_thisger.net"
>>> m = re.search("remove_this", email)
>>> email[:m.start()] + email[m.end():]
'tony@tiger.net'
match.span([group])

Para uma correspondência m, retorna a tupla de dois (m.start(group), m.end(group)). Observe que se group não contribuiu para a correspondência, isso é (-1, -1). group tem como padrão zero, a correspondência inteira.

match.pos

The value of pos which was passed to the search() or match() method of a regex object. This is the index into the string at which the RE engine started looking for a match.

match.endpos

The value of endpos which was passed to the search() or match() method of a regex object. This is the index into the string beyond which the RE engine will not go.

match.lastindex

O índice em número inteiro do último grupo de captura correspondido, ou None se nenhum grupo foi correspondido. Por exemplo, as expressões (a)b, ((a)(b)) e ((ab)) terão lastindex == 1 se aplicadas à string 'ab', enquanto a expressão (a)(b) terá lastindex == 2, se aplicada à mesma string.

match.lastgroup

O nome do último grupo de captura correspondido, ou None se o grupo não tinha um nome, ou se nenhum grupo foi correspondido.

match.re

The regular expression object whose match() or search() method produced this match instance.

match.string

The string passed to match() or search().

6.2.5. Exemplos de expressão regular

6.2.5.1. Verificando por um par

In this example, we’ll use the following helper function to display match objects a little more gracefully:

def displaymatch(match):
    if match is None:
        return None
    return '<Match: %r, groups=%r>' % (match.group(), match.groups())

Suponha que você esteja escrevendo um programa de pôquer onde a mão de um jogador é representada como uma string de 5 caracteres com cada caractere representando uma carta, “a” para ás, “k” para rei, “q” para dama, “j” para valete, “t” para 10 e “2” a “9” representando a carta com esse valor.

To see if a given string is a valid hand, one could do the following:

>>> valid = re.compile(r"^[a2-9tjqk]{5}$")
>>> displaymatch(valid.match("akt5q"))  # Valid.
"<Match: 'akt5q', groups=()>"
>>> displaymatch(valid.match("akt5e"))  # Invalid.
>>> displaymatch(valid.match("akt"))    # Invalid.
>>> displaymatch(valid.match("727ak"))  # Valid.
"<Match: '727ak', groups=()>"

That last hand, "727ak", contained a pair, or two of the same valued cards. To match this with a regular expression, one could use backreferences as such:

>>> pair = re.compile(r".*(.).*\1")
>>> displaymatch(pair.match("717ak"))     # Pair of 7s.
"<Match: '717', groups=('7',)>"
>>> displaymatch(pair.match("718ak"))     # No pairs.
>>> displaymatch(pair.match("354aa"))     # Pair of aces.
"<Match: '354aa', groups=('a',)>"

To find out what card the pair consists of, one could use the group() method of the match object in the following manner:

>>> pair.match("717ak").group(1)
'7'

# Error because re.match() returns None, which doesn't have a group() method:
>>> pair.match("718ak").group(1)
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    re.match(r".*(.).*\1", "718ak").group(1)
AttributeError: 'NoneType' object has no attribute 'group'

>>> pair.match("354aa").group(1)
'a'

6.2.5.2. Simulando scanf()

Python atualmente não possui um equivalente a scanf(). Expressões regulares são geralmente mais poderosas, embora também mais detalhadas, do que strings de formato scanf(). A tabela abaixo oferece alguns mapeamentos mais ou menos equivalentes entre os tokens de formato scanf() e expressões regulares.

Token scanf() Expressão regular
%c .
%5c .{5}
%d [-+]?\d+
%e, %E, %f, %g [-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?
%i [-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)
%o [-+]?[0-7]+
%s \S+
%u \d+
%x, %X [-+]?(0[xX])?[\dA-Fa-f]+

Para extrair um nome de arquivo e números de uma string como

/usr/sbin/sendmail - 0 errors, 4 warnings

você usaria um formato de scanf() como

%s - %d errors, %d warnings

A expressão regular equivalente seria

(\S+) - (\d+) errors, (\d+) warnings

6.2.5.3. search() vs. match()

Python oferece duas operações primitivas diferentes baseadas em expressões regulares: re.match() verifica se há uma correspondência apenas no início da string, enquanto re.search() verifica se há uma correspondência em qualquer lugar da string (isto é o que o Perl faz por padrão).

Por exemplo:

>>> re.match("c", "abcdef")    # No match
>>> re.search("c", "abcdef")   # Match
<_sre.SRE_Match object; span=(2, 3), match='c'>

Expressões regulares começando com '^' podem ser usadas com search() para restringir a correspondência no início da string:

>>> re.match("c", "abcdef")    # No match
>>> re.search("^c", "abcdef")  # No match
>>> re.search("^a", "abcdef")  # Match
<_sre.SRE_Match object; span=(0, 1), match='a'>

Note however that in MULTILINE mode match() only matches at the beginning of the string, whereas using search() with a regular expression beginning with '^' will match at the beginning of each line.

>>> re.match('X', 'A\nB\nX', re.MULTILINE)  # No match
>>> re.search('^X', 'A\nB\nX', re.MULTILINE)  # Match
<_sre.SRE_Match object; span=(4, 5), match='X'>

6.2.5.4. Criando uma lista telefônica

split() divide uma string em uma lista delimitada pelo padrão passado. O método é inestimável para converter dados textuais em estruturas de dados que podem ser facilmente lidas e modificadas pelo Python, conforme demonstrado no exemplo a seguir que cria uma lista telefônica.

First, here is the input. Normally it may come from a file, here we are using triple-quoted string syntax:

>>> text = """Ross McFluff: 834.345.1254 155 Elm Street
...
... Ronald Heathmore: 892.345.3428 436 Finley Avenue
... Frank Burger: 925.541.7625 662 South Dogwood Way
...
...
... Heather Albrecht: 548.326.4584 919 Park Place"""

As entradas são separadas por uma ou mais novas linhas. Agora, convertemos a string em uma lista com cada linha não vazia tendo sua própria entrada:

>>> entries = re.split("\n+", text)
>>> entries
['Ross McFluff: 834.345.1254 155 Elm Street',
'Ronald Heathmore: 892.345.3428 436 Finley Avenue',
'Frank Burger: 925.541.7625 662 South Dogwood Way',
'Heather Albrecht: 548.326.4584 919 Park Place']

Finalmente, divida cada entrada em uma lista com nome, sobrenome, número de telefone e endereço. Usamos o parâmetro maxsplit de split() porque o endereço contém espaços, nosso padrão de divisão:

>>> [re.split(":? ", entry, 3) for entry in entries]
[['Ross', 'McFluff', '834.345.1254', '155 Elm Street'],
['Ronald', 'Heathmore', '892.345.3428', '436 Finley Avenue'],
['Frank', 'Burger', '925.541.7625', '662 South Dogwood Way'],
['Heather', 'Albrecht', '548.326.4584', '919 Park Place']]

O padrão :? corresponde ao caractere de dois pontos após o sobrenome, de modo que não ocorre na lista de resultados. Com um maxsplit de 4, podemos separar o número da casa do nome da rua:

>>> [re.split(":? ", entry, 4) for entry in entries]
[['Ross', 'McFluff', '834.345.1254', '155', 'Elm Street'],
['Ronald', 'Heathmore', '892.345.3428', '436', 'Finley Avenue'],
['Frank', 'Burger', '925.541.7625', '662', 'South Dogwood Way'],
['Heather', 'Albrecht', '548.326.4584', '919', 'Park Place']]

6.2.5.5. Manipulação de texto

sub() substitui cada ocorrência de um padrão por uma string ou o resultado de uma função. Este exemplo demonstra o uso de sub() com uma função para manipular o texto ou aleatorizar a ordem de todos os caracteres em cada palavra de uma frase, exceto o primeiro e o último caracteres:

>>> def repl(m):
...     inner_word = list(m.group(2))
...     random.shuffle(inner_word)
...     return m.group(1) + "".join(inner_word) + m.group(3)
>>> text = "Professor Abdolmalek, please report your absences promptly."
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
'Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.'
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
'Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.'

6.2.5.6. Encontrando todos os advérbios

findall() matches all occurrences of a pattern, not just the first one as search() does. For example, if one was a writer and wanted to find all of the adverbs in some text, he or she might use findall() in the following manner:

>>> text = "He was carefully disguised but captured quickly by police."
>>> re.findall(r"\w+ly", text)
['carefully', 'quickly']

6.2.5.7. Encontrando todos os advérbios e suas posições

If one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. Continuing with the previous example, if one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner:

>>> text = "He was carefully disguised but captured quickly by police."
>>> for m in re.finditer(r"\w+ly", text):
...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
07-16: carefully
40-47: quickly

6.2.5.8. Notação de string bruta

Raw string notation (r"text") keeps regular expressions sane. Without it, every backslash ('\') in a regular expression would have to be prefixed with another one to escape it. For example, the two following lines of code are functionally identical:

>>> re.match(r"\W(.)\1\W", " ff ")
<_sre.SRE_Match object; span=(0, 4), match=' ff '>
>>> re.match("\\W(.)\\1\\W", " ff ")
<_sre.SRE_Match object; span=(0, 4), match=' ff '>

When one wants to match a literal backslash, it must be escaped in the regular expression. With raw string notation, this means r"\\". Without raw string notation, one must use "\\\\", making the following lines of code functionally identical:

>>> re.match(r"\\", r"\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>
>>> re.match("\\\\", r"\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>

6.2.5.9. Escrevendo um tokenizador

Um tokenizador, tokenizer ou scanner analisa uma string para categorizar grupos de caracteres. Este é um primeiro passo útil para escrever um compilador ou interpretador.

As categorias de texto são especificadas com expressões regulares. A técnica é combiná-las em uma única expressão regular mestre e fazer um loop em correspondências sucessivas:

import collections
import re

Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])

def tokenize(code):
    keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}
    token_specification = [
        ('NUMBER',  r'\d+(\.\d*)?'),  # Integer or decimal number
        ('ASSIGN',  r':='),           # Assignment operator
        ('END',     r';'),            # Statement terminator
        ('ID',      r'[A-Za-z]+'),    # Identifiers
        ('OP',      r'[+\-*/]'),      # Arithmetic operators
        ('NEWLINE', r'\n'),           # Line endings
        ('SKIP',    r'[ \t]+'),       # Skip over spaces and tabs
        ('MISMATCH',r'.'),            # Any other character
    ]
    tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
    line_num = 1
    line_start = 0
    for mo in re.finditer(tok_regex, code):
        kind = mo.lastgroup
        value = mo.group(kind)
        if kind == 'NEWLINE':
            line_start = mo.end()
            line_num += 1
        elif kind == 'SKIP':
            pass
        elif kind == 'MISMATCH':
            raise RuntimeError('%r unexpected on line %d' % (value, line_num))
        else:
            if kind == 'ID' and value in keywords:
                kind = value
            column = mo.start() - line_start
            yield Token(kind, value, line_num, column)

statements = '''
    IF quantity THEN
        total := total + price * quantity;
        tax := price * 0.05;
    ENDIF;
'''

for token in tokenize(statements):
    print(token)

O tokenizador produz a seguinte saída:

Token(typ='IF', value='IF', line=2, column=4)
Token(typ='ID', value='quantity', line=2, column=7)
Token(typ='THEN', value='THEN', line=2, column=16)
Token(typ='ID', value='total', line=3, column=8)
Token(typ='ASSIGN', value=':=', line=3, column=14)
Token(typ='ID', value='total', line=3, column=17)
Token(typ='OP', value='+', line=3, column=23)
Token(typ='ID', value='price', line=3, column=25)
Token(typ='OP', value='*', line=3, column=31)
Token(typ='ID', value='quantity', line=3, column=33)
Token(typ='END', value=';', line=3, column=41)
Token(typ='ID', value='tax', line=4, column=8)
Token(typ='ASSIGN', value=':=', line=4, column=12)
Token(typ='ID', value='price', line=4, column=15)
Token(typ='OP', value='*', line=4, column=21)
Token(typ='NUMBER', value='0.05', line=4, column=23)
Token(typ='END', value=';', line=4, column=27)
Token(typ='ENDIF', value='ENDIF', line=5, column=4)
Token(typ='END', value=';', line=5, column=9)