Regular Expressions in Oracle

1

'Select from the CITIES tables only records with compound names separated by a single space'

TABLE CIDADES

ID  CIDADE
01  SAO PAULO
02  RIO DE JANEIRO
03  SAO CARLOS
04  TAUBATÉ
05  SÃO JOSÉ DO RIO PRETO

Query can only return

TABLE CIDADES

ID  CIDADE
01  SAO PAULO
03  SAO CARLOS

HOW TO WRITE A REGULAR EXPRESSION THAT RETURNS ME TO COMPOSED CITIES FOR JUST 2 NAMES?

    
asked by anonymous 31.03.2017 / 14:54

2 answers

2

I'll assume we do this by the number of spaces ...

 select * from cidades 
 where regexp_count(cidade,' ')=1
    
31.03.2017 / 14:59
1

For cases where there is more than one space, one option is this:

SELECT * FROM CIDADES 
WHERE REGEXP_COUNT(CIDADE, '[[:alpha:]]\s{1,}[[:alpha:]]') =  1;
    
31.03.2017 / 16:05