메뉴 건너뛰기

조회 수 2861 추천 수 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
번호 제목 날짜 조회 수
132 전자정부 표준프레임워크 설치하기 file 2016.08.29 4486
131 자바용 날짜 계산 2016.09.19 4755
130 자바에서 문자열 비교 시 == 가 아닌 equals를 써야하는 이유 file 2023.02.14 445
129 자바 필드, 멤버 변수, 전역 변수는 같은 말? file 2023.02.14 357
128 자바 클래스와 메서드 2023.02.15 374
127 자바 클래스, 객체, 인스턴스 구분하기 file 2023.02.14 341
126 자바 초기화는 무슨 뜻이고 왜 해야할까? file 2023.02.14 409
» 자바 정규식 마스킹처리 file 2018.06.26 2861
124 자바 오버라이드, 오버로드 차이 알아보기 file 2023.02.14 384
123 자바 쓰레드 예제 및 사용 이유 알아보기 file 2023.02.14 366
122 자바 생성자란 무엇인가? file 2023.02.14 375
121 자바 배열 복사하는 방법 file 2023.02.14 377
120 자바 메소드(Method)란 무엇인가? file 2023.02.14 393
119 자바 랜덤 함수(Java random) file 2019.03.05 1021
118 자바 대소문자 확인하는 방법 file 2023.02.14 373
117 자바 다양한 형변환. 그리고 아스키 코드 String char int : JAVA 2016.12.09 4396
116 자바 날짜 포맷 변환 방법 file 2018.06.21 1689
115 자바 객체화(인스턴스화) 알아보기 file 2023.02.14 337
114 자바 XML 제어 라이브러리 XStream : JAVA 2016.12.09 5150
113 자바 Thread dump file 2023.02.15 366
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved