基于hackerrank问题的带有regexp的Oracle sql

abithluo  于 2023-01-18  发布在  Oracle
关注(0)|答案(1)|浏览(118)

我这样做

select distinct city 
from station 
where regexp_like(city,'^[^aeiou].*[^aeiou]$','i') and regexp_like(city,'^[^aeiou]\w+[^aeiou]$','i');

我知道这是错的。但是有人能根据这个问题给我解释一下我错在哪里吗?任何答案都将不胜感激。谢谢。
这里链接屏幕截图从问题。

xtupzzrd

xtupzzrd1#

它必须是regex吗?substr以简单的方式处理它:

SQL> with station (city) as
  2    (select 'Zagreb' from dual union all
  3     select 'Athens' from dual union all
  4     select 'Rome'   from dual union all
  5     select 'Ottawa' from dual
  6    )
  7  select distinct city
  8      from station
  9      where upper(substr(city, 1, 1)) not in ('A', 'E', 'I', 'O', 'U')
 10        and upper(substr(city, -1))   not in ('A', 'E', 'I', 'O', 'U');

CITY
------
Zagreb

SQL>

如果必须使用regex,则

7  select distinct city
  8      from station
  9      where regexp_like(city, '^[^aeiou].*[^aeiou]$', 'i');

CITY
------
Zagreb

SQL>

相关问题