메뉴 건너뛰기

조회 수 2637 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {

	public static void main(String[] args) {
		
		System.out.println(phoneNum("01012345678"));
		System.out.println(name("왕효상"));
		System.out.println(email("dress07@test.com"));
		System.out.println(ip("127.0.0.1"));
		
	}
	
	public static String phoneNum(String str) {
		String replaceString = str;
		
		Matcher matcher = Pattern.compile("^(\\d{3})-?(\\d{3,4})-?(\\d{4})$").matcher(str);
	
		if(matcher.matches()) {
			replaceString = "";
			
			boolean isHyphen = false;
			if(str.indexOf("-") > -1) {
				isHyphen = true;
			}
			
			for(int i=1;i<=matcher.groupCount();i++) {
				String replaceTarget = matcher.group(i);
				if(i == 2) {
					char[] c = new char[replaceTarget.length()];
					Arrays.fill(c, '*');
					
					replaceString = replaceString + String.valueOf(c);	
				} else {
					replaceString = replaceString + replaceTarget;
				}
				
				if(isHyphen && i < matcher.groupCount()) {
					replaceString = replaceString + "-";
				}
			}
		}
		
		return replaceString;
	}
	
	public static String name(String str) {
		String replaceString = str;
		
		String pattern = "";
		if(str.length() == 2) {
			pattern = "^(.)(.+)$";
		} else {
			pattern = "^(.)(.+)(.)$";
		}
		
		Matcher matcher = Pattern.compile(pattern).matcher(str);
	
		if(matcher.matches()) {
			replaceString = "";
			
			for(int i=1;i<=matcher.groupCount();i++) {
				String replaceTarget = matcher.group(i);
				if(i == 2) {
					char[] c = new char[replaceTarget.length()];
					Arrays.fill(c, '*');
					
					replaceString = replaceString + String.valueOf(c);
				} else {
					replaceString = replaceString + replaceTarget;
				}
				
			}
		}
		
		return replaceString;
	}
	
	public static String email(String str) {
		String replaceString = str;
		
		Matcher matcher = Pattern.compile("^(..)(.*)([@]{1})(.*)$").matcher(str);
		
		if(matcher.matches()) {
			replaceString = "";
			
			for(int i=1;i<=matcher.groupCount();i++) {
				String replaceTarget = matcher.group(i);
				if(i == 2) {
					char[] c = new char[replaceTarget.length()];
					Arrays.fill(c, '*');
					
					replaceString = replaceString + String.valueOf(c);
				} else {
					replaceString = replaceString + replaceTarget;
				}
			}
			
		}
		
		return replaceString;
	}
	
	public static String ip(String str) {
		String replaceString = str;
		
		Matcher matcher = Pattern.compile("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$").matcher(str);
		
		if(matcher.matches()) {
			replaceString = "";
			
			for(int i=1;i<=matcher.groupCount();i++) {
				String replaceTarget = matcher.group(i);
				if(i == 3) {
					char[] c = new char[replaceTarget.length()];
					Arrays.fill(c, '*');
					
					replaceString = replaceString + String.valueOf(c);
				} else {
					replaceString = replaceString + replaceTarget;
				}
				if(i < matcher.groupCount()) {
					replaceString =replaceString + "."; 
				}
			}
		}
		
		return replaceString;
	}

}



List of Articles
번호 제목 날짜 조회 수
31 Gmail 메일 서버를 이용해서 메일 보내기 file 2020.06.29 258
30 jstl <c:url value=""> 사용시 ;jsessionid= 붙는 현상 file 2021.03.31 232
29 TCP 소켓 프로그래밍 01 - Server/Client 일대일 연결 file 2021.03.31 178
28 자바 - 공백 문자 제거하기 (trim, replaceAll) file 2021.03.31 173
27 [객체 지향 언어의 이해] 업캐스팅과 다운캐스팅 file 2021.03.31 158
26 자바 대소문자 확인하는 방법 file 2023.02.15 134
25 HashMap 사용하기 file 2021.03.31 134
24 Reflection을 활용한 메서드, 필드 값 불러오기. 2021.03.31 124
23 국제 시간에 따른 날짜 출력 2020.06.29 122
22 자바에서 문자열 비교 시 == 가 아닌 equals를 써야하는 이유 file 2023.02.15 115
21 자바 String Class 문자열 처리 함수에 대한 정리 2021.03.31 108
20 쓰레드 (Thread) 사용하기 file 2021.03.31 105
19 request header 로부터 접속 정보 확인 file 2023.02.15 87
18 자바 초기화는 무슨 뜻이고 왜 해야할까? file 2023.02.15 86
17 자바 클래스와 메서드 2023.02.15 79
16 자바 extends Thread, implements Runnable 차이 file 2023.02.15 79
15 자바 메소드(Method)란 무엇인가? file 2023.02.15 78
14 Singleton Pattern 과 DeadLock file 2023.02.15 77
13 자바 extends, implements 차이점 알아보기 file 2023.02.15 76
12 자바 필드, 멤버 변수, 전역 변수는 같은 말? file 2023.02.15 75
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved