Clase 7

Temas

  • Regex
  • XML

Regex

  • ¿Qué? Cadenas que describen un conjunto
  • ¿Cómo? Utilizando símbolos y operadores
  • ¿Para qué? Búsqueda y reemplazo de cadenas

Símbolos:

  • Literales del lenguaje + operadores

Operaciones:

  • Alternancia: A o B
  • Cuantificación: Cuánto espero de un elemento
  • Agrupación: Elementos que se puedan referenciar

Alfabeto:

  • ASCII (8 bit char) - Unicode

Librería:

import re
In [15]:
import re
help(re)
Help on module re:

NAME
    re - Support for regular expressions (RE).

FILE
    /Users/Matias/miniconda2/lib/python2.7/re.py

MODULE DOCS
    http://docs.python.org/library/re

DESCRIPTION
    This module provides regular expression matching operations similar to
    those found in Perl.  It supports both 8-bit and Unicode strings; both
    the pattern and the strings being processed can contain null bytes and
    characters outside the US ASCII range.
    
    Regular expressions can contain both special and ordinary characters.
    Most ordinary characters, like "A", "a", or "0", are the simplest
    regular expressions; they simply match themselves.  You can
    concatenate ordinary 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.
                 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 RE.
        {m,n}?   Non-greedy version of the above.
        "\\"     Either escapes special characters or signals a special sequence.
        []       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 parentheses.
                 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<name>...) The substring matched by the group is accessible by name.
        (?P=name)     Matches the text matched earlier by the group named name.
        (?#...)  A comment; ignored.
        (?=...)  Matches if ... matches next, but doesn't consume the string.
        (?!...)  Matches if ... doesn't match next.
        (?<=...) Matches if preceded by ... (must be fixed length).
        (?<!...) Matches if not preceded by ... (must be fixed length).
        (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                           the (optional) no pattern otherwise.
    
    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].
        \D       Matches any non-digit character; equivalent to the set [^0-9].
        \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v].
        \S       Matches any non-whitespace character; equiv. to [^ \t\n\r\f\v].
        \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_].
                 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.
    
    This module exports the following functions:
        match    Match a regular expression pattern to the beginning of a string.
        search   Search a string for the presence of a pattern.
        sub      Substitute occurrences of a pattern found in a string.
        subn     Same as sub, but also return the number of substitutions made.
        split    Split a string by the occurrences of a pattern.
        findall  Find all occurrences of a pattern in a string.
        finditer Return an iterator yielding a match object for each match.
        compile  Compile a pattern into a RegexObject.
        purge    Clear the regular expression cache.
        escape   Backslash all non-alphanumerics in a string.
    
    Some of the functions in this module takes flags as optional parameters:
        I  IGNORECASE  Perform case-insensitive matching.
        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 RE's.
        U  UNICODE     Make \w, \W, \b, \B, dependent on the Unicode locale.
    
    This module also defines an exception 'error'.

CLASSES
    exceptions.Exception(exceptions.BaseException)
        sre_constants.error
    
    class error(exceptions.Exception)
     |  Method resolution order:
     |      error
     |      exceptions.Exception
     |      exceptions.BaseException
     |      __builtin__.object
     |  
     |  Data descriptors defined here:
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from exceptions.Exception:
     |  
     |  __init__(...)
     |      x.__init__(...) initializes x; see help(type(x)) for signature
     |  
     |  ----------------------------------------------------------------------
     |  Data and other attributes inherited from exceptions.Exception:
     |  
     |  __new__ = <built-in method __new__ of type object>
     |      T.__new__(S, ...) -> a new object with type S, a subtype of T
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from exceptions.BaseException:
     |  
     |  __delattr__(...)
     |      x.__delattr__('name') <==> del x.name
     |  
     |  __getattribute__(...)
     |      x.__getattribute__('name') <==> x.name
     |  
     |  __getitem__(...)
     |      x.__getitem__(y) <==> x[y]
     |  
     |  __getslice__(...)
     |      x.__getslice__(i, j) <==> x[i:j]
     |      
     |      Use of negative indices is not supported.
     |  
     |  __reduce__(...)
     |  
     |  __repr__(...)
     |      x.__repr__() <==> repr(x)
     |  
     |  __setattr__(...)
     |      x.__setattr__('name', value) <==> x.name = value
     |  
     |  __setstate__(...)
     |  
     |  __str__(...)
     |      x.__str__() <==> str(x)
     |  
     |  __unicode__(...)
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from exceptions.BaseException:
     |  
     |  __dict__
     |  
     |  args
     |  
     |  message

FUNCTIONS
    compile(pattern, flags=0)
        Compile a regular expression pattern, returning a pattern object.
    
    escape(pattern)
        Escape all non-alphanumeric characters in pattern.
    
    findall(pattern, string, flags=0)
        Return a list of all non-overlapping matches in the string.
        
        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.
    
    finditer(pattern, string, flags=0)
        Return an iterator over all non-overlapping matches in the
        string.  For each match, the iterator returns a match object.
        
        Empty matches are included in the result.
    
    match(pattern, string, flags=0)
        Try to apply the pattern at the start of the string, returning
        a match object, or None if no match was found.
    
    purge()
        Clear the regular expression cache
    
    search(pattern, string, flags=0)
        Scan through string looking for a match to the pattern, returning
        a match object, or None if no match was found.
    
    split(pattern, string, maxsplit=0, flags=0)
        Split the source string by the occurrences of the pattern,
        returning a list containing the resulting substrings.
    
    sub(pattern, repl, string, count=0, flags=0)
        Return the string obtained by replacing the leftmost
        non-overlapping occurrences of the pattern in string by the
        replacement repl.  repl can be either a string or a callable;
        if a string, backslash escapes in it are processed.  If it is
        a callable, it's passed the match object and must return
        a replacement string to be used.
    
    subn(pattern, repl, string, count=0, flags=0)
        Return a 2-tuple containing (new_string, number).
        new_string is the string obtained by replacing the leftmost
        non-overlapping occurrences of the pattern in the source
        string by the replacement repl.  number is the number of
        substitutions that were made. repl can be either a string or a
        callable; if a string, backslash escapes in it are processed.
        If it is a callable, it's passed the match object and must
        return a replacement string to be used.
    
    template(pattern, flags=0)
        Compile a template pattern, returning a pattern object

DATA
    DOTALL = 16
    I = 2
    IGNORECASE = 2
    L = 4
    LOCALE = 4
    M = 8
    MULTILINE = 8
    S = 16
    U = 32
    UNICODE = 32
    VERBOSE = 64
    X = 64
    __all__ = ['match', 'search', 'sub', 'subn', 'split', 'findall', 'comp...
    __version__ = '2.2.1'

VERSION
    2.2.1


In [31]:
import re

match = re.match("a(b?)+","a")
if not match:
    raise ValueError("No match :(")
print match
print match.re
print match.string
print match.group()
print match.start()
print match.end()
<_sre.SRE_Match object at 0x1043937b0>
<_sre.SRE_Pattern object at 0x10418d8c8>
a
a
0
1

Sintaxis expresiones regulares

  • Una expresión regular (o RE) especifica un set de cadenas que 'matchean' con ella
  • Las funciones en este modulo permiten chequear si una cadena en particular 'matchea' con una expresion regular dada (o viceversa)
  • Las expresiones regulares pueden contener caracteres especiales u ordinarios.
  • Los ordinarios son por ejemplo: 'A', 'a' o '0' son las RE mas comunes y coinciden con ellas mismas.
  • Se pueden concatenar varios caracteres ordinarios por ejemplo: 'last' que coincide con la cadena 'last'.

Caracteres especiales

Los siguientes caracteres especiales matchean con:

  • . con cualquier caracter excepto newline
  • ^ con el comienzo de una cadena, si se encuentra en modo MULTILINE también lo hace con cada newline
  • $ con el fin de la cadena, o justo antes de una newline
  • * matchea 0 o mas veces con la RE que lo precede: ab* matchea con a, ab, o a seguido de cualquier número de b's
  • + matchea mas de 0 veces con la RE que lo precede: ab+ matchea con ab, o a seguido de cualquier número (distinto de 0) de b's
  • ? ab? con a o ab
  • {m} exactamente con m copias de la RE que lo precede. a{6} solo matchea con aaaaaa
  • {m,n}? matchea desde m a n repeticiones, tratando de que sean la menor cantidad posible. Ej: aaaaaa, a{3,5} con aaaaa mientras que a{3,5}? solo con aaa
  • \ permite matchear caracteres especiales como *, ?
  • A|B siendo A y B cualquier RE, matchea con A o B. Para matchear el literal | usar \|

Como tener un set de caracteres como RE

  • [] permite indicar un set de caracteres
  • Pueden ser listados individualmente [amk] matchea con a o m o k
  • [-] para indicar rangos: [a-z] matchea con cualquier caracter entre a y z, [0-5][0-9] con cualquier numero de dos dígitos desde 00 a 59
  • Los caracteres especiales pierden su significado dentro de sets

Match vs Search:

  • re.match(patron, cadena, flags): busca coincidencias solo al principio de la cadena
  • re.search(patron, cadena, flags): busca coincidencias en toda la cadena
In [35]:
import re

cadena = "Cats are smarter than dogs"
#matchObj = re.match('Cat', cadena)
#matchObj = re.match('dogs', cadena)
matchObj = re.search('dogs', cadena)
if matchObj:
    print matchObj.group()
else:
    print "No match!"
dogs

Veamos un ejemplo

In [77]:
def regex_nombre(nombre):
    regex = "([a-zA-Z]+) (([a-zA-Z]+) )?([a-zA-Z]+)"
#    regex = "([a-zA-Z]+){2,3}?"
    match = re.match(regex, nombre)
    if match:
        print "Nombre completo: %s" % (match.group(0))

        print "Primer nombre: %s" % (match.group(1))
        
        print "Segundo nombre: %s" % (match.group(2))
        
        print "Tercer nombre: %s" % (match.group(4))
    else:
        print "No match :("
        
regex_nombre("Miguel Alfaro")
#regex_nombre("Miguel 1523")
Nombre completo: Miguel Alfaro
Primer nombre: Miguel
Segundo nombre: None
Tercer nombre: Alfaro

Ejercicio 7.1

Escribir una función que recibe una cadena con el mes y el dia de una fecha dada de la forma Mes dia, por ejemplo: Marzo 3. E indique cual es la fecha entera, el mes y el dia.

>>>regex_fecha("Junio 3")
Fecha: Junio 3
Mes: Junio
Dia: 3
In [72]:
import re

def regex_fecha(fecha):
    regex = "([A-Z][a-z]+) ([0-3][0-1])"
    match = re.search(regex, fecha)
    if match:
        print "Fecha: %s" % (match.group(0))

        print "Mes: %s" % (match.group(1))

        print "Dia: %s" % (match.group(2))
    else:
        print "No match :("
        
regex_fecha("Junio 3")
Fecha: Junio 3
Mes: Junio
Dia: 3

Ejercicio 7.2

Escribir una función que reciba un mail y diga si es válido o no

In [80]:
import re

def regex_mail(mail):
    regex = "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    match = re.search(regex, mail)
    return match is not None

regex_mail("@miguelalfaro@gmail.com")
Out[80]:
False

Ejercicio 7.3

Escribir una función que reciba una lista de saldos de cuentas y devuelva otra lista con aquellos que son negativos usando regex

In [91]:
def regex_saldos(saldos):
    regex = "-"
    saldos_negativos = []
#    for saldo in saldos:
#        match = re.search(regex, saldo)
#        if match:
#            saldos_negativos.append(saldo)
    filtro_saldos = lambda saldo: re.match(regex, saldo)
    return filter(filtro_saldos, saldos)
#    return saldos_negativos

regex_saldos(["-123","123","-42","32"])
    
Out[91]:
['-123', '-42']

Ejercicio 7.4

Escribir una funcion que reciba una lista de DNI y devuelva True si todos son DNI's validos.

  • Los DNI tienen 8 dígitos
In [109]:
def regex_dni(dnis):
    regex = r"\d{8}"
    for dni in dnis:
        match = re.match(regex, dni)
        if not match:
            return False
    return True

print regex_dni(["37569522", "17594345", "12543645"])
print regex_dni(["37569522", "175", "12543645"])
True
False

XML

Soporte para DOM y SAX

  • Seis librerías:
    • xml.etree.ElementTree
    • xml.dom
    • xml.dom.minidom
    • xml.dom.pulldom
    • xml.sax
    • xml.parsers.expat

Diferencias:

  • ElementTree: una interfaz simple y optimizada.
  • DOM - Minidom: una API que sigue la implementación de W3C
  • SAX: API que sigue la implementación SAX de Java
In [95]:
import xml.etree.ElementTree as ET

a = ET.Element('a')
a.set("name","A")
b = ET.SubElement(a, 'Soy un hijo de a')
c = ET.SubElement(a, 'c')
d = ET.SubElement(c, 'Y yo soy hijo de c')
tree = ET.ElementTree(a)
ET.dump(tree)
tree.write("archivo_prueba.xml")
print a.attrib
<a name="A"><Soy un hijo de a /><c><Y yo soy hijo de c /></c></a>
{'name': 'A'}

Ejercicio 7.5

Implementar un script que transforme el archivo materias.txt en xml.

El archivo materias.txt tiene el formato:

6201 - FISICA I A - 60
...
<CD><CM> - <NOMBRE> - <CUPOS>

El formato esperado debe ser:

<root>
        <departamento codigo="62">
            <materia codigo="01" nombre="Física I" cupos="60"/> .....
            <materia codigo="cc" nombre="nombre" cupos="xx"/>
        </departamento>
    </root>

Nota: En caso de ser necesario ver help.

In [106]:
import os
import re
import xml.etree.ElementTree

class Materia(object):
    def __init__(self, nombre, codigo, cupos):
        self.nombre = nombre
        self.codigo = codigo
        self.cupos = cupos

    def __str__(self):
        return self.nombre + "\t" + self.codigo + "\t" + str(self.cupos)

def exportar_materias_por_departamento():
    # Cambia el directorio de trabajo.
    pattern = r"^(?P<depto>\d{2})(?P<codigo>\d{2}) - (?P<nombre>[\w\s]+) - (?P<cupos>\d+)$"
    regex = re.compile(pattern)
    path = os.path.join(os.getcwd(), "..", "Archivos de prueba")
    os.chdir(path)

    # Itera por departamento.
    materias_por_depto = {}
    with open("materias.txt") as materias:
        for materia in materias:
            match = regex.match(materia)
            if match is None:
                continue
            depto = match.group("depto")
            codigo = match.group("codigo")
            cupos = match.group("cupos")
            nombre = match.group("nombre")
            materias_por_depto[depto] = materias_por_depto.get(depto, []) + [ Materia(nombre, codigo, int(cupos)) ]
    root = xml.etree.ElementTree.Element("root")
    tree = xml.etree.ElementTree.ElementTree(root)	
    for depto in materias_por_depto:
        xml_depto = xml.etree.ElementTree.SubElement(root, "departamento")
        xml_depto.set("codigo", depto)
        for materia in materias_por_depto[depto]:
            xml_materia = xml.etree.ElementTree.SubElement(xml_depto, "materia")
            xml_materia.set("codigo", materia.codigo)
            xml_materia.set("nombre", materia.nombre)
            xml_materia.set("cupos", str(materia.cupos))
    tree.write("materias.xml")

exportar_materias_por_departamento()

Ejercicio 7.6

Implementar un programa que levante parametros.txt, reciba por parámetro el nombre del atributo a cambiar y guarde el nuevo archivo de configuración como xml.