정규식을 이용한 주민등록번호 추출

|
정규식을 이용한 주민등록 번호 추출 프로그램
RRNTest.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//\d\d(0[1-9]|1[0-2])([0-2][1-9]|3[0-1])[1-4]\d{6}

//Regular Expression
class RRN {
	String rrn;
	String PatternStr = "\\d\\d(0[1-9]|1[0-2])([0-2][1-9]|3[0-1])[1-4]\\d{6}";
	Pattern pat = Pattern.compile(PatternStr);

	Matcher mat;
	boolean found;
	boolean more = true;

	public String FindRRN(String str) {
		mat = pat.matcher(str);
		found = mat.find();
		if (found)
		{
			rrn = mat.group();
			more = true;
			//end()는 mat.group() 후에 사용 가능
			str = str.substring(mat.end());
		}
		else
		{
			rrn = "Not found";
			more = false;
		}
		return str;
	}
}

public class RRNTest {
	public static void main(String[] args) {
		String str = "8204254093614abc128714cd7903241081511AE@5605047145666#";
		boolean more = true;
		RRN r = new RRN();
		while (r.more) {
			str = r.FindRRN(str);
			if (r.rrn != "Not found") System.out.println(r.rrn);
		}
	}
}


-

'Java' 카테고리의 다른 글

java  (0) 2010.08.31
쓰레드  (0) 2010.08.26
오토박싱/언박싱  (0) 2010.08.24
Stream  (0) 2010.08.23
Exception  (0) 2010.08.23
And