본문 바로가기
TIL - 프로그래밍/SQL

[SQL] 정규표현식 - 해커랭크 Weather Observation Station 6~11

by chaemj97 2023. 7. 7.
728x90
 

RegexOne - Learn Regular Expressions - Lesson 1: An Introduction, and the ABCs

Regular expressions are extremely useful in extracting information from text such as code, log files, spreadsheets, or even documents. And while there is a lot of theory behind formal languages, the following lessons and examples will explore the more prac

regexone.com

 

RegExr: Learn, Build, & Test RegEx

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

regexr.com


정규표현식을 사용할 때 REGEXP를 사용

Weather Observation Station 6

  • CITY의 첫 글자가 모음으로 시작하는 CITY 출력하라
    • ^ : 문자열의 시작
    • [char...] : 문자 리스트 중 한 문자와 일치
    • . : 모든 문자와 일치
    • * : 0회 또는 그 이상의 횟수로 일치
SELECT DISTINCT CITY
FROM STATION
WHERE LEFT(CITY,1) IN ('A','E','I','O','U');

-- 정규표현식
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[aeiou].*';

 

Weather Observation Station 7

  • CITY의 마지막 글자가 모음으로 시작하는 CITY 출력하라
    • $ : 문자열의 끝
SELECT DISTINCT CITY
FROM STATION
WHERE RIGHT(CITY,1) IN ('A','E','I','O','U');

-- 정규표현식
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '.*[aeiou]$';

 

Weather Observation Station 8

  • CITY의 첫 글자와 마지막 글자가 모음인 CITY 출력하라
SELECT DISTINCT CITY
FROM STATION
WHERE RIGHT(CITY,1) IN ('A','E','I','O','U') 
    AND LEFT(CITY,1) IN ('A','E','I','O','U');

-- 정규표현식
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[aeiou].*[aeiou]$';

 

Weather Observation Station 9

  • CITY의 첫 글자가 모음이 아닌 CITY 출력하라
    • [^char...] : 문자 리스트에 어떤 문자도 포함하지 않는다.
SELECT DISTINCT CITY
FROM STATION
WHERE LEFT(CITY,1) NOT IN ('A','E','I','O','U');

-- 정규표현식
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[^aeiou].*';

 

Weather Observation Station 10

  • CITY의 마지막 글자가 모음이 아닌 CITY 출력하라
SELECT DISTINCT CITY
FROM STATION
WHERE RIGHT(CITY,1) NOT IN ('A','E','I','O','U');

-- 정규표현식
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '.*[^aeiou]$';

 

Weather Observation Station 11

  • CITY의 첫 글자가 모음이 아니거나 마지막 글자가 모음이 아닌 CITY 출력하라
SELECT DISTINCT CITY
FROM STATION
WHERE LEFT(CITY,1) NOT IN ('A','E','I','O','U')
OR RIGHT(CITY,1) NOT IN ('A','E','I','O','U');

-- 정규표현식
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[^aeiou].*'
OR CITY REGEXP '[^aeiou]$';
728x90
반응형

댓글