개발/컴파일러

Regular expression(정규표현식)

SiJun-Park 2024. 9. 30. 23:39

사용한 API는 Pattern Api를 사용 하였습니다.

목표는 Java regular expression을 이용하여 input.txt에 존재하는 모든 문자열을 어떤 문자열 종류인지 구분하는 프로그램을 제작하는 것 입니다.

  1. 핸드폰 번호 : XXX-XXXX-XXXX
  2. 이메일 주소 : [문자 또는 _ ] @ [도메인] *여러개의 .[문자]가 존재함

ex) 가능한 극단적 예시 : ____ @aaaa.aaaa.aa

안되는 예시 : ____@test...........a

3. 핸드폰 종류 : [iPhone or Galaxy] [숫자 1~ 3자리] (핸드폰 제조사 뒤에 스페이스 한 칸 존재)

4. 파일 종류 : [문자 또는 _].[확장자] (확장자는 4가지만 .c .java .py .ml )

 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;

import java.util.regex.Pattern;
public class Main {


    public  static boolean phone_number_check(String text){
        return Pattern.matches("^[0-9]{3}-[0-9]{4}-[0-9]{4}$", text);
        //XXX-XXXX-XXXX니깐, 3번 반복, 4번 반복, 4번 반복을 체크를 해줍니다. 이때 범위는 0-9범위를 체크 해줍니다.
    }
    public  static boolean email_check(String text){
        return Pattern.matches("^([a-zA-Z0-9]|_)+@[a-zA-Z]+(\\.[a-zA-Z]+)+", text);
        //test123@test.com형식 입니다. 이때 .com.com이 가능하니 .a-zA-Z가 한개 이상되도록 합니다. 이때 (.com)이 한쌍이 되도록 합니다.
    }
    public  static boolean phone_type_check(String text){
        return Pattern.matches("^(iPhone|Galaxy)\\s+[0-9]{1,3}+", text);
        //iPhone 123 , Galaxy 123를 체크 합니다. 즉 기종 숫자를 체크합니다. 반복범위는 3까지만
    }

    public  static boolean File_type_check(String text){
        return Pattern.matches("^([a-zA-Z0-9]|_)+(\\.(c|java|py|ml))", text);
        //파일을 체크합니다. 이때 파일명하고 .c or java or py or ml을 체크합니다.
    }

    public static void main(String[] args) throws IOException {
        String read; // 문자열을 읽기 위해 선언 합니다.
        BufferedReader reader = new BufferedReader( new FileReader("./input.txt")); // input file read


                while ((read = reader.readLine()) != null) {
                String[] text = read.split("[\n]+"); // \n기준으로 문자열을 잘라 줍니다.
                for(String current : text) { // 한줄씩 읽음습니다.
                    if(phone_number_check(current)) System.out.println("Match : The input string is ["+current+"] Matched Type is [Phone Number]");
                    else if(email_check(current))System.out.println("Match : The input string is ["+current+"] Matched Type is [e-mail]");
                    else if(phone_type_check(current))System.out.println("Match : The input string is ["+current+"] Matched Type is [Phone Type]");
                    else if(File_type_check(current))System.out.println("Match : The input string is ["+current+"] Matched Type is [Source File]");
                    else System.out.println("Error : The input does not belong to the given string type ["+current+"]");

                }
            }
    }
}

작성한 코드입니다.

이전 과제와 같이 문자열로 쪼개 주었고, 쪼갠 문자열을 얻어와 체크를 하여서 print를 하는 형식으로 하였습니다.

결과

input

output


input

output


input

output


input

output


input

output