SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Regex Programming Porn
Les regex comme vous ne avez jamais vus
Human Talks Lyon, 08/10/13 / @gautier_difolco
regular expressions is #programming #porn, you
always feel guilty and dirty after that :'(
Deux solutions :
Plus de pr0n
Plus de Regex
Notes
Présentation des regex PCRE (et non POSIX)
Nous parlerons de regex pour éviter le débat expressions régulières
vs expressions rationnelles
Présentation et environnement de benchmarks disponibles sur
https://github.com/blackheaven/regex_programming_porn
Plan
Constitutifs de base
Changements du comportement d'évalutation
Un exemple qui fait saigner du nez
Constitutifs de base
/PATTERN/OPTIONS
/at/
A cat eats a black fat rat
Alternative monocaractère
/[cft]/
A cat eats a black fat rat
Exclusion monocaractère
/[^a]/
A cat eats a black fat rat
/[^rt]/
A cat eats a black fat rat
Multiplicité
0 ou 1 /at?/- /at{0,1}/- /at{,1}/
A cat eats a black fat rat
0 à inf. /at*/- /at{0,}/
A cat eats a black fat rat
1 à inf. /at+/- /at{1,}/
A cat eats a black fat rat
n à m /at{n,m}/
A cat eats a black fat rat
n /at{n}/
A cat eats a black fat rat
Classes de caractères
Intervals /[a-z]/
Hier, j'ai terminé à 6h00.
Classes prédefinies /[w]/vs /[[:alnum]]/
Hier, j'ai terminé à 6h00.
Jocker /.W/
Hier, j'ai_terminé_à_6h00.
Non-équivalences à cause des accents dans certains langages
Accents /[=e=]/
Hier, j'ai terminé à 6h00.
Capture / Alternatives multi-
caractères
/(ca|ts)/
A cat eats a black fat rat
/(.)(?:la|at)/
A cat eats a black fat rat
En PCRE /(bla|blac)/
A cat eats a black fat rat
En POSIX /(bla|blac)/
A cat eats a black fat rat
Changements du
comportement d'évalutation
Ancres
Début /^./
A cat eats a black fat rat
Fin /.$/
A cat eats a black fat rat
Délimiteur de mots /.atb/
A cat eats a black fat rat
Qui est différent de l'espace /.ats/
A cat_eats a black fat_rat$
Options
/PATTERN/OPTIONS
Insensibilité à la casse /a/i
A cat eats a black fat rat
Prise en compte des retours à la ligne des ancres /at$/m
A catn
eats a black fat rat$
Prise en compte des retours pour le point /at./svoire /at./s
A catn
eats a black fat_rat
Commentaires
/at#Chercheat/x
A cat eats a black fat rat
Comportements glouton
Chercher la plus longue correspondance /a.*t/
A cat eats a black fat rat
Chercher la plus courte correspondance /a.*?t/
A cat eats a black fat rat
Références aux
correspondances
Les correspondances capturées sont accessible via 1 à 9
Chercher les balise à contenu /<(w+)>.*</1>/
<p></br></p>
Motifs prospectifs
Positif /(?=.*[A-Z])(?=.*d).{5,}/
MyPassword
Négatif /(?=.*[A-Z])(?!.*d).{5,}/
MyPassword
Motifs retrospectifs
Positif /(?<=if) (true|false)/
if true while false
Négatif /(?<!if)(true|false)/
if true while false
Motifs conditionnels
/if (true)?(?(1)(?!else)|false else true)/
if true then false
if true
if false then true
/if (true)?(?(1)(?! else))/
if true then false
if true
if false
Et plus encore
Motifs retardés (récursifs) en Perl
Motifs autonomes ("super-glouton") en JAVA
Un exemple
/b(?:[w_][w_d]*)b(?
<!if|else|while|for|return)s*b([w_]
[w_d]*)b(?!s*()(?!.*b1b)/
/
b
(?:[w_][w_d]*)#Groupe1
b
(?<!if|else|while|for|return)#Rétrospectifnégatif1
s*
b
([w_][w_d]*)#Groupe2
b
(?!s*()#Prospectifnégatif1
(?!.*b1b) #Prospectifnégatif2
/xm
/
b
(?:[w_][w_d]*)#Identificateurdetypenoncapturé
b
(?<!if|else|while|for|return)#quin'estpasunmotclef
s*
b
([w_][w_d]*)#Identificateurcapturé
b
(?!s*()#Prospectifnégatif1
(?!.*b1b) #Prospectifnégatif2
/xm
/
b
(?:[w_][w_d]*)#Identificateurdetypenoncapturé
b
(?<!if|else|while|for|return)#quin'estpasunmotclef
s*
b
([w_][w_d]*)#Identificateurcapturé
b
(?!s*()#quin'estpaslenomd'unefonction
(?!.*b1b) #Prospectifnégatif2
/xm
/
b
(?:[w_][w_d]*)#Identificateurdetypenoncapturé
b
(?<!if|else|while|for|return)#quin'estpasunmotclef
s*
b
([w_][w_d]*)#Identificateurcapturé
b
(?!s*()#quin'estpaslenomd'unefonction
(?!.*b1b) #etquineréapparaîtplus
/xm
/
b
(?:[w_][w_d]*)#Identificateurdetypenoncapturé
b
(?<!if|else|while|for|return)#quin'estpasunmotclef
s*
b
([w_][w_d]*)#Identificateurcapturé
b
(?!s*()#quin'estpaslenomd'unefonction
(?!.*b1b) #etquineréapparaîtplus
/xm
Liste les variables déclarées mais non utilisées
Conclusion
Utile pour extraire/valide des informations basiques (addressses
email, dates, unités lexicalles)
Nombreuses variations en fonction du langage ou de la bibliothèque
Un "vrai" analyseur lexical/syntaxique devient rapidement vital
Questions ?
Pour continuer l'entrainement : http://regexcrossword.com/

Mais conteúdo relacionado

Destaque

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Regex programming porn