Corrispondenza di pattern

Pulizia dei dati nei database PostgreSQL

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

Identificare pattern: un esempio

 camis    |              name               | inspection_date | score | nta  | ... 
 ---------+---------------------------------+-----------------+-------+------+-----
 ...      | ...                             | ...             | ...   | ...  | ...
 41659848 | LA BRISA DEL CIBAO              | 01/30/2018      | 20    | QN26 | ...
 40961447 | MESON SEVILLA RESTAURANT        | 03/19/2019      | 50    | MN15 | ...
 50063071 | WA BAR                          | 05/23/2018      | 15    | MN17 | ...
 50034992 | EMPANADAS MONUMENTAL            | 06/21/2019      | 17    | MN35 | ...
 50095871 | ALPHONSO'S PIZZERIA & TRATTORIA | 01/16/2020      | 10    | MN28 | ...
 41104041 | THE SPARROW TAVERN              | 09/17/2019      | 13    | QN72 | ...
 50016937 | BURGER KING                     | 09/14/2018      | 12    | QN55 | ...
 50066469 | DARBAR'S CHICKEN & RIBS         | 08/07/2017      | 11    | QN55 | ...
 41195691 | F & J PINE RESTAURANT           | 05/02/2019      | 26    | BX49 | ...
 50015706 | EL RINCONCITO DE LOS SABORES    | 12/18/2019      | 20    | QN35 | ...
 ...      | ...                             | ..    | ...  | ...
1 https://www1.nyc.gov/site/planning/data-maps/open-data/dwn-nynta.page
Pulizia dei dati nei database PostgreSQL

Identificare pattern: un esempio

Prefissi validi per il codice NTA

  • MN - Manhattan
  • BK - Brooklyn
  • BX - Bronx
  • QN - Queens
  • SI - Staten Island
Pulizia dei dati nei database PostgreSQL

Identificare pattern: un esempio

Risultati tabella con un riquadro rosso che evidenzia un codice NTA non valido

Pulizia dei dati nei database PostgreSQL

L'operatore LIKE

SELECT * FROM restaurant_inspection WHERE nta LIKE 'QN544';
SELECT * FROM restaurant_inspection WHERE nta = 'QN544';
Pulizia dei dati nei database PostgreSQL

L'operatore LIKE

Caratteri per pattern matching

  • % - corrisponde a qualsiasi sequenza di zero o più caratteri

  • _ (underscore) - corrisponde a un singolo carattere

SELECT 
* 
FROM 
restaurant_inspection 
WHERE nta LIKE 'QN%';

 

 

 

SELECT 
* 
FROM 
restaurant_inspection 
WHERE nta LIKE 'QN%' 
AND nta NOT LIKE 'QN__';
Pulizia dei dati nei database PostgreSQL

Espressioni regolari (RE)

  • Il pattern matching con LIKE è limitato
  • Pattern più specifici possono essere utili
  • Le espressioni regolari (RE) permettono pattern più espressivi
Pulizia dei dati nei database PostgreSQL

L'operatore SIMILAR TO

  • Offre funzionalità aggiuntive di pattern matching
    SELECT 
      camis, name, inspection_date, score, nta 
    FROM 
      restaurant_inspection 
    WHERE nta SIMILAR TO 'QN%' AND nta NOT SIMILAR TO 'QN__';
    
Pulizia dei dati nei database PostgreSQL

Basi delle RE

Metacarattere Uso Esempio RE Esempio di match
\d corrisponde a una cifra (0-9) \d\d\d '345'
? corrisponde a 0 o 1 del carattere precedente x\d? 'x5'
+ corrisponde a una o più del carattere precedente \d+ '10'
* corrisponde a qualsiasi carattere 0 o più volte \d* '3081'
[] corrisponde a un carattere tra le parentesi [a-z] 'f'
Pulizia dei dati nei database PostgreSQL

Uso delle RE con SIMILAR TO

SELECT 
    camis, name, inspection_date, score, nta 
FROM 
    restaurant_inspection 
WHERE nta SIMILAR TO 'QN%' AND nta NOT SIMILAR TO 'QN__';
SELECT 
    camis, name, inspection_date, score, nta 
FROM 
    restaurant_inspection 
WHERE 
    nta NOT SIMILAR TO '[A-Z][A-Z]\d\d';
 camis    |        name        | inspection_date | score | nta  
 ---------+--------------------+-----------------+-------+------
 41659848 | LA BRISA DEL CIBAO | 01/30/2018      | 20    | Q26
 41104041 | THE SPARROW TAVERN | 09/17/2019      | 13    | QN723
Pulizia dei dati nei database PostgreSQL

Passons à la pratique !

Pulizia dei dati nei database PostgreSQL

Preparing Video For Download...