expressões regulares para novatos, usando python

Download Expressões regulares para novatos, usando Python

Post on 16-Apr-2017

81 views

Category:

Technology

1 download

Embed Size (px)

TRANSCRIPT

  • Globalcode Open4education

    Expresses Regulares para novatos, usando Python

    Amom Mendes

  • Globalcode Open4education

    Agenda

    O que so expresses regulares Para que servem? Metacaracteres Notebook

  • Globalcode Open4education

    Agenda

    O que so expresses regulares Para que servem? Metacaracteres Notebook

  • Globalcode Open4education

    Agenda

    O que so expresses regulares Para que servem? Metacaracteres Notebook

  • Globalcode Open4education

    Agenda

    O que so expresses regulares Para que servem? Metacaracteres Notebook (talvez!)

  • Globalcode Open4education

    Motivao

  • Globalcode Open4education

    Motivao

  • Globalcode Open4education

    O que so Expresses regulares?

  • Globalcode Open4education

    O que so Expresses regulares?

    Mtodo formal de especificar um padro de texto

    import re

  • Globalcode Open4education

    O que so Expresses regulares?

    Hoje tem trilha de Python no TDC Caracteres literais

  • Globalcode Open4education

    O que so Expresses regulares?

    Hoje tem trilha de Python no TDC

    re.search (r(?i)(python).*(tdc))

    Caracteres literais

    Regra(Smb. + Carac. Fn. Especiais)

  • Globalcode Open4education

    O que so Expresses regulares?

    Hoje tem trilha de Python no TDC

    re.search (r(?i)(python).*(tdc))

    Python no tdc

    Caracteres literais

    Regra(Smb. + Carac. Fn. Especiais)

    Match (casou)

  • Globalcode Open4education

    Para que servem? Muita | Qualquer coisa relacionada a busca de padres de caracteres

    Router de pginas (ex. django) Datas / Horrios Nomes E-mails Web Scrapping Consulta bancos de dados Substituio de caracteres Etc, etc

  • Globalcode Open4education

    Por onde comear?

  • Globalcode Open4education

    Metacaracteres

    Principais

    . ? * + ^ $ | [ ] { } ( ) \

  • Globalcode Open4education

    Metacaracteres

    Simplificando

    ^$\b

    Representantes Quantificadores

    ncoras Outros

    . [ ][^] ?*+{ }

    \ | (..) \1

  • Globalcode Open4education

    Metacaracteres

    .Mtodo findall: retorna um lista com textos casados( )

    Representantes

  • Globalcode Open4education

    Metacaracteres

    .re.findall(r'n.o', #Expresso

    'no, nao, neo, nso, nzo,nap')#String

    ['no', 'nao', 'neo', 'nso', 'nzo']

    Representantes

  • Globalcode Open4education

    Metacaracteres

    [...] Representantes

  • Globalcode Open4education

    Metacaracteres

    re.findall (r'[nao]', 'no, nao, neo')

    ['n', '', 'o', 'n', 'a', 'o', 'n', 'o']

    Representantes

  • Globalcode Open4education

    Metacaracteres

    m = re.search(r'[0-9][0-9]:[0-9][0-9]', 'Cheguei as 11:00 horas no TDC hoje')

    m.group()

    Representantes

  • Globalcode Open4education

    Metacaracteres

    m = re.search(r'[0-9][0-9]:[0-9][0-9]', 'Cheguei as 11:00 horas no TDC hoje')

    m.group()

    Mtodo search: testa se casou e retorna um objeto com informaes do match( )

    Representantes

  • Globalcode Open4education

    Metacaracteres

    m = re.search(r'[0-9][0-9]:[0-9][0-9]', 'Cheguei as 11:00 horas no TDC hoje')

    m.group()

    11:00

    Representantes

  • Globalcode Open4education

    m = re.findall(r'[^A-z ], 'Cheguei as 11:00 horas no TDC hoje')

    ['1', '1', ':', '0', '0']

    Metacaracteres Representantes

  • Globalcode Open4education

    ?Metacaracteres Quantificadores

  • Globalcode Open4education

    re.findall (r'fala[r!]?', 'falar, fala!, fala')

    ['falar', 'fala!', 'fala']

    Metacaracteres Quantificadores

  • Globalcode Open4education

    *Metacaracteres Quantificadores

  • Globalcode Open4education

    re.findall (r'Ah*!', 'Ah!, Ahhh!, Ahhhh!, Ahhhhhhhhh!')

    ['Ah!', 'Ahhh!', 'Ahhhh!', 'Ahhhhhhhhh!']

    Metacaracteres Quantificadores

  • Globalcode Open4education

    .*re.findall (r'Ah.*!', 'Ah!, Ahhh!, Ahhhh!,A(h * 1000)!')

    'Ah!, Ahhh!, Ahhhh!,A(h * 1000)!'

    Metacaracteres Quantificadores

  • Globalcode Open4education

    +if re.search(r'Ah+!', 'A!'):

    print (Casou)else:

    print (No Casou)

    No Casou

    Metacaracteres Quantificadores

  • Globalcode Open4education

    { }Metacaracteres Quantificadores

  • Globalcode Open4education

    re.findall (r'Ah{1,3}!','Ah!, Ahhh!, Ahhhh!, Ahhhhhhhhh!')

    ['Ah!', 'Ahhh!']

    Metacaracteres Quantificadores

  • Globalcode Open4education

    Metacaracteres Ancoras

  • Globalcode Open4education

    = fome!

    Metacaracteres Ancoras

  • Globalcode Open4education

    notebook

  • Globalcode Open4education

    Obrigado!amommendes@gmail.com