import re
def solution(new_id):
answer = ''
step1 = new_id.lower()
step2 = re.sub('[^a-z0-9\.\-\_]+', '', step1)
step3 = re.sub('[\.]+', '.', step2)
step4 = re.sub('^\.|\.$','', step3)
step5 = "a" if len(step4) == 0 else step4
step6 = step5[:15] if len(step5) > 15 else step5
step6 = re.sub('\.$', '', step6)
step7 = step6
while(len(step7)<3):
step7 = f'{step7}{step7[-1]}'
answer = step7
return answer
새로운 정규식 표현을 배웠음
re.sub('r pattern', 'replacement string', 'string', 바꿀 횟수)
: 정규표현식에서 특정 문자열을 찾은 뒤 다른 문자열로 바꾸는 함수
^
, $
: 문자열의 시작과 끝을 가리킴
^
뒤에 오는 문자는 문자열의 시작 문자와 매칭된다. $
앞에 오는 문자는 문자열의 끝 문자와 매칭된다. |
: 둘 이상의 항목에서 선택 (or)
(z|f)ood -> zood, food
[]
: 문자 클래스 = []
사이의 문자들과 매치
** []
안에 메타문자 ^
를 사용하는 경우 반대라는 의미를 갖는다
{}
: 수량을 표시