SlideShare a Scribd company logo
1 of 6
LUIGI CAPUZZELLO

RegEx
Le 10 regole di base più importanti.
Versione: 1.0

Luigi Capuzzello
17/12/2013

http://www.linkedin.com/pub/luigi-capuzzello/7/561/12a
http://www.slideshare.net/luigicapuzzello
@FisherKasparov
luigi.capuzzello

Il documento contiene la descrizione delle dieci regole di base più importanti per poter iniziare a utilizzare
in maniera effettiva le Regular Expression.
Regular Expression2

Sommario
Regular Expression...............................................................................................................................3
Esempi base......................................................................................................................................4
Caratteri Speciali..............................................................................................................................4
Ripetizioni........................................................................................................................................4
Classi di caratteri..............................................................................................................................5
Negazione.........................................................................................................................................5
Alternative........................................................................................................................................5

Luigi Capuzzello
Regular Expression3

Regular Expression
Una espressione regolare definisce una funzione che prende in ingresso una stringa, e restituisce in uscita
un valore del tipo sì/no, a seconda che la stringa segua o meno un certo pattern.
Ad esempio, tutti gli indirizzi e-mail devono essere costituiti nel seguente modo: cominciare con una
sequenza di caratteri alfanumerici, seguiti dal simbolo chiocciola, seguiti da altri caratteri alfanumerici,
seguiti dal punto, seguiti da due o tre lettere.[1] Questa regola informale diventerebbe una regex qualora
fosse codificata secondo una sintassi ben precisa e riconosciuta da un programma in grado di analizzare le
stringhe.
Una espressione regolare (in lingua inglese regular expression o, in forma abbreviata, regexp, regex o RE) è
una sequenza di simboli (quindi una stringa) che identifica un insieme di stringhe. Programmi diversi
supportano notazioni diverse per esprimere le stesse espressioni regolari, pertanto non esiste
una sintassi "universale".

Luigi Capuzzello
Regular Expression4

Esempi base
belvisb

Cerco la parola elvis all’interno di una frase (stringa)
^elvis$

Cerco la parola elvis all’interno di una sola riga
belvisb.*baliveb

Cerco la parola elvis seguita dalla parola alive.
o .
indica tutti i caratteri (a parte invio)
o ripeti il carattere precdente quante volte vuoi

bddd-dddd
bd{3}-d{4}

Voglio trovare un numero tipo xxx-xxxx
o {3}
ripeti tre volte il carattere precedente

baw*b

Una parola che inizia per la lettera a

Caratteri Speciali
.
w
s
d
b
^
$

Match any character except newline
Match any alphanumeric character
Match any whitespace character
Match any digit
Match the beginning or end of a word
Match the beginning of the string
Match the end of the string

Ripetizioni
*
+
?
{n}
{n,m}
{n,}

Repeat any number of times
Repeat one or more times
Repeat zero or one time
Repeat n times
Repeat at least n, but no more than m times
Repeat at least n times

Esempio
bw{5,6}b

Find all five and six letter words
bd{3}sd{3}-d{4}
Find ten digit phone numbers
d{3}-d{2}-d{4}
Social security number
^w* The first word in the line or in the text
Luigi Capuzzello
Regular Expression5

Classi di caratteri
Posso creare il mio set di caratteri
[aeiou]: È la classe che definisce le vocali
[.?!]: è la classe che definisce i simboli di punteggiatura (i caratteri ?. perdono il significato speciale)
[a-z0-9] posso specificare un range di caratteri

Negazione
W
S
D
B
[^x]
[^aeiou]

Match any character that is NOT alphanumeric
Match any character that is NOT whitespace
Match any character that is NOT a digit
Match a position that is NOT the beginning or end of a word
Match any character that is NOT x
Match any character that is NOT one of the characters aeiou

Alternative
bd{5}-d{4}b|bd{5}b

Five and nine digit Zip Codes

Luigi Capuzzello
Regular Expression5

Classi di caratteri
Posso creare il mio set di caratteri
[aeiou]: È la classe che definisce le vocali
[.?!]: è la classe che definisce i simboli di punteggiatura (i caratteri ?. perdono il significato speciale)
[a-z0-9] posso specificare un range di caratteri

Negazione
W
S
D
B
[^x]
[^aeiou]

Match any character that is NOT alphanumeric
Match any character that is NOT whitespace
Match any character that is NOT a digit
Match a position that is NOT the beginning or end of a word
Match any character that is NOT x
Match any character that is NOT one of the characters aeiou

Alternative
bd{5}-d{4}b|bd{5}b

Five and nine digit Zip Codes

Luigi Capuzzello

More Related Content

Featured

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
ThinkNow
 
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
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
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...
 

Regular expression

  • 1. LUIGI CAPUZZELLO RegEx Le 10 regole di base più importanti. Versione: 1.0 Luigi Capuzzello 17/12/2013 http://www.linkedin.com/pub/luigi-capuzzello/7/561/12a http://www.slideshare.net/luigicapuzzello @FisherKasparov luigi.capuzzello Il documento contiene la descrizione delle dieci regole di base più importanti per poter iniziare a utilizzare in maniera effettiva le Regular Expression.
  • 2. Regular Expression2 Sommario Regular Expression...............................................................................................................................3 Esempi base......................................................................................................................................4 Caratteri Speciali..............................................................................................................................4 Ripetizioni........................................................................................................................................4 Classi di caratteri..............................................................................................................................5 Negazione.........................................................................................................................................5 Alternative........................................................................................................................................5 Luigi Capuzzello
  • 3. Regular Expression3 Regular Expression Una espressione regolare definisce una funzione che prende in ingresso una stringa, e restituisce in uscita un valore del tipo sì/no, a seconda che la stringa segua o meno un certo pattern. Ad esempio, tutti gli indirizzi e-mail devono essere costituiti nel seguente modo: cominciare con una sequenza di caratteri alfanumerici, seguiti dal simbolo chiocciola, seguiti da altri caratteri alfanumerici, seguiti dal punto, seguiti da due o tre lettere.[1] Questa regola informale diventerebbe una regex qualora fosse codificata secondo una sintassi ben precisa e riconosciuta da un programma in grado di analizzare le stringhe. Una espressione regolare (in lingua inglese regular expression o, in forma abbreviata, regexp, regex o RE) è una sequenza di simboli (quindi una stringa) che identifica un insieme di stringhe. Programmi diversi supportano notazioni diverse per esprimere le stesse espressioni regolari, pertanto non esiste una sintassi "universale". Luigi Capuzzello
  • 4. Regular Expression4 Esempi base belvisb Cerco la parola elvis all’interno di una frase (stringa) ^elvis$ Cerco la parola elvis all’interno di una sola riga belvisb.*baliveb Cerco la parola elvis seguita dalla parola alive. o . indica tutti i caratteri (a parte invio) o ripeti il carattere precdente quante volte vuoi bddd-dddd bd{3}-d{4} Voglio trovare un numero tipo xxx-xxxx o {3} ripeti tre volte il carattere precedente baw*b Una parola che inizia per la lettera a Caratteri Speciali . w s d b ^ $ Match any character except newline Match any alphanumeric character Match any whitespace character Match any digit Match the beginning or end of a word Match the beginning of the string Match the end of the string Ripetizioni * + ? {n} {n,m} {n,} Repeat any number of times Repeat one or more times Repeat zero or one time Repeat n times Repeat at least n, but no more than m times Repeat at least n times Esempio bw{5,6}b Find all five and six letter words bd{3}sd{3}-d{4} Find ten digit phone numbers d{3}-d{2}-d{4} Social security number ^w* The first word in the line or in the text Luigi Capuzzello
  • 5. Regular Expression5 Classi di caratteri Posso creare il mio set di caratteri [aeiou]: È la classe che definisce le vocali [.?!]: è la classe che definisce i simboli di punteggiatura (i caratteri ?. perdono il significato speciale) [a-z0-9] posso specificare un range di caratteri Negazione W S D B [^x] [^aeiou] Match any character that is NOT alphanumeric Match any character that is NOT whitespace Match any character that is NOT a digit Match a position that is NOT the beginning or end of a word Match any character that is NOT x Match any character that is NOT one of the characters aeiou Alternative bd{5}-d{4}b|bd{5}b Five and nine digit Zip Codes Luigi Capuzzello
  • 6. Regular Expression5 Classi di caratteri Posso creare il mio set di caratteri [aeiou]: È la classe che definisce le vocali [.?!]: è la classe che definisce i simboli di punteggiatura (i caratteri ?. perdono il significato speciale) [a-z0-9] posso specificare un range di caratteri Negazione W S D B [^x] [^aeiou] Match any character that is NOT alphanumeric Match any character that is NOT whitespace Match any character that is NOT a digit Match a position that is NOT the beginning or end of a word Match any character that is NOT x Match any character that is NOT one of the characters aeiou Alternative bd{5}-d{4}b|bd{5}b Five and nine digit Zip Codes Luigi Capuzzello