[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(); } }
최근 댓글