본문 바로가기
프로그래머 ,백준, 유튜브, 문제

신규아이디 추천_프로그래머스

by 리승우 2022. 9. 27.

[필수개념]

> 정규화, 꼭 알아야함

class Solution {
    public String solution(String new_id) {
        String answer = "";
        String temp = new_id.toLowerCase();

        //^ : 앞의 문자들 제외
        // 제외하고 모두 ""으로 처리하겠다
        temp = temp.replaceAll("[^-_.a-z0-9]","");

        //2개 이상의 .들을 모두 제외
        temp = temp.replaceAll("[.]{2,}",".");

        //첫자리 끝자리 . 들 모두 제외
        temp = temp.replaceAll("^[.]|[.]$","");

        //문자열 공백이면 "a"추가
        if(temp.equals(""))
            temp+="a";

        //문자열 16이상이면 15까지만 출력하고 첫자리 끝자리 . 제외
        if(temp.length() >=16){
            temp = temp.substring(0,15);
            temp=temp.replaceAll("^[.]|[.]$","");
        }

        // 문자열 2개 이하이면 끝자리 추가
        if(temp.length()<=2)
            while(temp.length()<3)
                temp+=temp.charAt(temp.length()-1);

        answer=temp;
        return answer;
    }
}


    public class Hello {
        public static void main(String[] args) {
            Solution test = new Solution();
            String o = "...!@BaT#*..y.abcdefghijklm";
            System.out.println(test.solution(o));
        }
    }

댓글