dura regex sed regex
por wairton de abreu
2
3
Stephen Cole Kleene
(1909 - 1994)
o que é uma expressão regular?
4
import re
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M',
'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE',
'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__',
'__file__', '__name__', '__package__', '__version__', '_alphanum',
'_cache', '_cache_repl', '_compile', '_compile_repl', '_expand',
'_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error',
'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split',
'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
5
import re
['A', '_alphanum_str', 'ASCII', 'fullmatch', '__loader__', 'copyreg',
'__cached__', '_alphanum_bytes', '__spec__']
6
o básico (parte 1) ? * + . 
“pessoas?” “faz(em)?”
“go+l” “gol” “goooooooooool”-> ou
7
.match() e .search()
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.
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.
8
Pattern → compile → Match
>>> pattern = re.compile("andr[eé](ia)?")
>>> match = pattern.match("andreia")
>>> match.group()
'andreia'
9
d, D, w, W, s e S
[a-zA-Z0-9_] [^a-zA-Z0-9_]
[ tn] [^ tn]
[a-zA-Z0-9_] [^a-zA-Z0-9_]
[0-9] [^0-9]
10
a barra e o r
section → section → section → r“section”
11
o básico (parte 2) | [ ] ( ) { } ^ $
12
(?i) (?s)
case insensitive
o . captura quebra de linha
13
b B
"eu sou um pythonista! Sim, python é melhor do que ..."
14
.finditer() e .findall()
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
(...)
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.
15
.split(), .sub() e .subn()
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...
16
a classe Match
['end', 'endpos', 'expand', 'group', 'groupdict', 'groups',
'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
17
e o unicode?
>>> re.search("κα "," γώ ε μι δ ς κα λήθεια κα ζωή")ὶ Ἐ ἰ ἡ ὁ ὸ ὶ ἡ ἀ ὶ ἡ
<_sre.SRE_Match object; span=(16, 19), match='κα '>ὶ
18
grupos
>>> p = re.compile('(a(b)c)d')
>>> m = p.match('abcd')
>>> m.group(0)
'abcd'
>>> m.group(1)
'abc'
>>> m.group(2)
'b'
19
grupos nomeados
from django.conf.urls import url
urlpatterns = [
url(r'^articles/2003/$', 'news.views.special_case_2003'),
url(r'^articles/(?P<year>[0-9]{4})/$', 'news.views.year_archive'),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',
'news.views.month_archive'),
]
20
o que faltou?
quantificadores possessivos
lookahead e lookbehind
backreference
21
para onde agora?
expressões regulares – uma abordagem divertida
expressões regulares cookbook
https://docs.python.org/3/howto/regex.html#regex-howto
https://docs.python.org/3/library/re.html
http://www.regular-expressions.info
22
?

Duralexsedregex

  • 1.
    dura regex sedregex por wairton de abreu
  • 2.
  • 3.
    3 Stephen Cole Kleene (1909- 1994) o que é uma expressão regular?
  • 4.
    4 import re ['DEBUG', 'DOTALL','I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
  • 5.
    5 import re ['A', '_alphanum_str','ASCII', 'fullmatch', '__loader__', 'copyreg', '__cached__', '_alphanum_bytes', '__spec__']
  • 6.
    6 o básico (parte1) ? * + . “pessoas?” “faz(em)?” “go+l” “gol” “goooooooooool”-> ou
  • 7.
    7 .match() e .search() 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. 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.
  • 8.
    8 Pattern → compile→ Match >>> pattern = re.compile("andr[eé](ia)?") >>> match = pattern.match("andreia") >>> match.group() 'andreia'
  • 9.
    9 d, D, w,W, s e S [a-zA-Z0-9_] [^a-zA-Z0-9_] [ tn] [^ tn] [a-zA-Z0-9_] [^a-zA-Z0-9_] [0-9] [^0-9]
  • 10.
    10 a barra eo r section → section → section → r“section”
  • 11.
    11 o básico (parte2) | [ ] ( ) { } ^ $
  • 12.
    12 (?i) (?s) case insensitive o. captura quebra de linha
  • 13.
    13 b B "eu souum pythonista! Sim, python é melhor do que ..."
  • 14.
    14 .finditer() e .findall() findall(pattern,string, flags=0) Return a list of all non-overlapping matches in the string. (...) 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.
  • 15.
    15 .split(), .sub() e.subn() 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...
  • 16.
    16 a classe Match ['end','endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
  • 17.
    17 e o unicode? >>>re.search("κα "," γώ ε μι δ ς κα λήθεια κα ζωή")ὶ Ἐ ἰ ἡ ὁ ὸ ὶ ἡ ἀ ὶ ἡ <_sre.SRE_Match object; span=(16, 19), match='κα '>ὶ
  • 18.
    18 grupos >>> p =re.compile('(a(b)c)d') >>> m = p.match('abcd') >>> m.group(0) 'abcd' >>> m.group(1) 'abc' >>> m.group(2) 'b'
  • 19.
    19 grupos nomeados from django.conf.urlsimport url urlpatterns = [ url(r'^articles/2003/$', 'news.views.special_case_2003'), url(r'^articles/(?P<year>[0-9]{4})/$', 'news.views.year_archive'), url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', 'news.views.month_archive'), ]
  • 20.
    20 o que faltou? quantificadorespossessivos lookahead e lookbehind backreference
  • 21.
    21 para onde agora? expressõesregulares – uma abordagem divertida expressões regulares cookbook https://docs.python.org/3/howto/regex.html#regex-howto https://docs.python.org/3/library/re.html http://www.regular-expressions.info
  • 22.