0
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', ...
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 ma...
8
Pattern → compile → Match
>>> pattern = re.compile("andr[eé](ia)?")
>>> match = pattern.match("andreia")
>>> match.group...
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.
...
15
.split(), .sub() e .subn()
sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the lef...
16
a classe Match
['end', 'endpos', 'expand', 'group', 'groupdict', 'groups',
'lastgroup', 'lastindex', 'pos', 're', 'regs...
17
e o unicode?
>>> re.search("κα "," γώ ε μι δ ς κα λήθεια κα ζωή")ὶ Ἐ ἰ ἡ ὁ ὸ ὶ ἡ ἀ ὶ ἡ
<_sre.SRE_Match object; span=(16...
18
grupos
>>> p = re.compile('(a(b)c)d')
>>> m = p.match('abcd')
>>> m.group(0)
'abcd'
>>> m.group(1)
'abc'
>>> m.group(2)...
19
grupos nomeados
from django.conf.urls import url
urlpatterns = [
url(r'^articles/2003/$', 'news.views.special_case_2003...
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...
22
?
Upcoming SlideShare
Loading in...5
×

Duralexsedregex

262

Published on

Published in: Software, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
262
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Duralexsedregex"

  1. 1. dura regex sed regex por wairton de abreu
  2. 2. 2
  3. 3. 3 Stephen Cole Kleene (1909 - 1994) o que é uma expressão regular?
  4. 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. 5 import re ['A', '_alphanum_str', 'ASCII', 'fullmatch', '__loader__', 'copyreg', '__cached__', '_alphanum_bytes', '__spec__']
  6. 6. 6 o básico (parte 1) ? * + . “pessoas?” “faz(em)?” “go+l” “gol” “goooooooooool”-> ou
  7. 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. 8 Pattern → compile → Match >>> pattern = re.compile("andr[eé](ia)?") >>> match = pattern.match("andreia") >>> match.group() 'andreia'
  9. 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. 10 a barra e o r section → section → section → r“section”
  11. 11. 11 o básico (parte 2) | [ ] ( ) { } ^ $
  12. 12. 12 (?i) (?s) case insensitive o . captura quebra de linha
  13. 13. 13 b B "eu sou um pythonista! Sim, python é melhor do que ..."
  14. 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. 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. 16 a classe Match ['end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
  17. 17. 17 e o unicode? >>> re.search("κα "," γώ ε μι δ ς κα λήθεια κα ζωή")ὶ Ἐ ἰ ἡ ὁ ὸ ὶ ἡ ἀ ὶ ἡ <_sre.SRE_Match object; span=(16, 19), match='κα '>ὶ
  18. 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. 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. 20. 20 o que faltou? quantificadores possessivos lookahead e lookbehind backreference
  21. 21. 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. 22. 22 ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×