HackerRank regex 문제들

청수동햄주먹·2023년 6월 17일
0

SQL 공부

목록 보기
3/8

sql regex

해커랭크에서는 프로그래머스에서 못봤던 regex를 이용하는 문제도 있었다

regexp

PatternDescription
*Matches zero or more instances of the preceding String
+Matches one or more instances of the preceding String
.Matches any single character
?Matches zero or one instance of the preceding Strings
^^ matches the beginning of a String
$$ matches the ending of a String
[abc]Matches any character listed in between the square brackets
[^abc]Matches any character not listed in between the square brackets
[A-Z]Matches any letter in uppercase
[a-z]Matches any letter in lowercase
[0-9]Matches any digit between 0-9
[[:<:]]Matches the beginning of words
[[:>:]]Matches the end of words
[:class:]Matches any character class
p1|p2|p3Mathes any of the specified pattern
{n}Matches n instances of the preceding element
{m,n}Matches m through n instances of the preceding element
  • 모음[AEIOU]으로 시작(^)하는 도시이름 출력하기

    SELECT DISTINCT CITY
     FROM STATION
     WHERE   CITY REGEXP '^[AEIOU]'
  • 모음[AEIOU]으로 끝($)나는 도시이름 출력하기

    SELECT DISTINCT CITY
     FROM STATION
     WHERE   CITY REGEXP '[AEIOU]$'
  • 모음[AEIOU]으로 시작(^)하지도 끝($)나지도 않는 도시이름 출력하기

    SELECT  DISTINCT CITY
    FROM    STATION
    WHERE   CITY NOT REGEXP '^[AEIOU]'
              AND CITY NOT REGEXP '[AEIOU]$'
    • or 이 아니라 and! 동시에 만족하도록


left/right(column, n)

  • 문자열의 총 길이가 n 작을 때는 총 문자열이 출력된다.

    last n characters: RIGHT(column, n)

    • 마지막 n개의 문자를 고를 때

    first n character: Left(column, n)

    • 처음 n개의 문자를 고를 때


숫자 ↔ 문자

CHAR(n)

아스키값 숫자를 문자로 바꿔준다. 65 -> A

ASCII('c')

문자를 아스키값으로 바꿔준다. A -> 65



문자의 인덱스 위치

CHARINDEX('c','str')

  • 0번째는 없음. 첫글자는 1, 두번째 글자는 2.
  • 문자열안에 문자가 여러개 있을 때 첫번째로 보이는 인덱스를 출력한다.
SELECT CHARINDEX('a','apple') # 1
SELECT CHARINDEX('p','apple') # 2


문자열 합치기

CONCAT('str1','str2', ... , string_n)

'str1' + 'str2' ... + 'str_n'

두개 이상의 문자열을 합쳐준다

CONCAT_WS(';', 'str1', 'str2', ... , string_n);

맨 처음의 separator로 문자열들을 분리하면서 합쳐준다

SELECT CONCAT('PANDA','FAMILY','LOVE');
# PANDAFAMILYLOVE

SELECT 'AI'+'LE'+'FU';
# AILEFU

SELECT CONCAT_WS(' : ','PANDA','FAMILY','CUTE');
# PANDA : FAMILY : CUTE
profile
코딩과 사별까지

0개의 댓글