[JAVA] 정규식으로 버전명 추출하기

[JAVA] 정규식으로 버전명 추출하기
java regex version

단, 첫번째 조건 1 은 버전형식이 아니라고 판단하여 못거름.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Scratch {
    public static void main(String[] args) {

        String appVersion = "4.4.1-DEV";
        Pattern p = Pattern.compile(".*?(\\d+(\\.\\d+)+)");

        Matcher m = p.matcher(appVersion);

        m = p.matcher(refineString("1"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("1.1"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("1.1.1"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("1.1.1.2"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("1.1.1.2.3"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("1.1.1-DEV"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("11.1.1"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("11.1.1-DEV"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("11.11.111-DEV"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("11.11.111.1.1.9-DEV"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("1.2.1 HTTP/1.1"));
        if(m.find()){
            System.out.println(m.group());
        }
        m = p.matcher(refineString("Music Player 5.18.524.14 Windows7 6.1"));
        if(m.find()){
            System.out.println(m.group());
        }
    }

    public static String refineString(String s){
        return s.replaceAll("[a-zA-Z]","").trim();
    }
}

 

You may also like...

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.