✅ 코드
1. 풀이
import re
def solution(new_id):
answer = new_id.lower()
answer = re.sub("[^a-z0-9\-\_\.]","",answer)
answer = re.sub("(\.)+", ".", answer)
answer = re.sub("(^\.)|(\.$)", "", answer)
answer = re.sub("^$", "a", answer)
answer = answer[:15]
answer = re.sub("(\.$)", "", answer)
while len(answer) <= 2:
answer += answer[-1]
return answer
2. 다른 사람의 코드
def solution(new_id):
answer = ''
new_id = new_id.lower()
for c in new_id:
if c.isalpha() or c.isdigit() or c in ['-', '_', '.']:
answer += c
while '..' in answer:
answer = answer.replace('..', '.')
if answer[0] == '.':
answer = answer[1:] if len(answer) > 1 else '.'
if answer[-1] == '.':
answer = answer[:-1]
if answer == '':
answer = 'a'
if len(answer) > 15:
answer = answer[:15]
if answer[-1] == '.':
answer = answer[:-1]
while len(answer) < 3:
answer += answer[-1]
return answer
☑️ 핵심 코드
1. re 모듈 sub() 메서드
2. 대소문자 치환: upper(), lower()
str = 'Hello'
str.upper()
> 'HELLO'
str.lower()
> 'hello'
3. 정규표현식