1 실행결과 #
무조건 소수점 2자리 필수 [0-9]*\.[0-9]{2}, 32, X [0-9]*\.[0-9]{2}, 32.1, X [0-9]*\.[0-9]{2}, 32.123, X [0-9]*\.[0-9]{2}, 32.12, O 소수점 자유 (있던 없던) [0-9]*\.?[0-9]*, 32, O [0-9]*\.?[0-9]*, 32a, X [0-9]*\.?[0-9]*, .123, O [0-9]*\.?[0-9]*, 3.123, O [0-9]*\.?[0-9]*, 333.123, O [0-9]*\.?[0-9]*, 333.1 23, X 소수점 위 0~3자리, 소수점 아래 0~2자리, 소수점 없을수도 있음 --> 아래2개패턴 or 조건 [0-9]{0,3}, 32, O [0-9]{0,3}, 3234, X [0-9]{0,3}, 32.3, X [0-9]{0,3}, 32.33, X [0-9]{0,3}, 32.334, X [0-9]{0,3}\.[0-9]{1,2}, 32, X [0-9]{0,3}\.[0-9]{1,2}, 3234, X [0-9]{0,3}\.[0-9]{1,2}, 32.3, O [0-9]{0,3}\.[0-9]{1,2}, 32.33, O [0-9]{0,3}\.[0-9]{1,2}, 32.334, X [0-9]{0,3}\.[0-9]{1,2}, .32, O [0-9]{0,3}\.[0-9]{1,2}, .3234, X [0-9]{0,3}\.[0-9]{1,2}, .32.3, X [0-9]{0,3}\.[0-9]{1,2}, .32.33, X [0-9]{0,3}\.[0-9]{1,2}, .32.334, X
2 src #
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { /** * @param args */ public static void main(String[] args) { System.out.println("무조건 소수점 2자리 필수"); String pattern = "[0-9]*\\.[0-9]{2}"; test(pattern, "32"); test(pattern, "32.1"); test(pattern, "32.123"); test(pattern, "32.12"); System.out.println("소수점 자유 (있던 없던)"); pattern = "[0-9]*\\.?[0-9]*"; test(pattern, "32"); test(pattern, "32a"); test(pattern, ".123"); test(pattern, "3.123"); test(pattern, "333.123"); test(pattern, "333.1 23"); System.out.println( "소수점 위 0~3자리, 소수점 아래 0~2자리, 소수점 없을수도 있음 --> 아래2개패턴 or 조건"); pattern = "[0-9]{0,3}"; test(pattern, "32"); test(pattern, "3234"); test(pattern, "32.3"); test(pattern, "32.33"); test(pattern, "32.334"); pattern = "[0-9]{0,3}\\.[0-9]{1,2}"; test(pattern, "32"); test(pattern, "3234"); test(pattern, "32.3"); test(pattern, "32.33"); test(pattern, "32.334"); test(pattern, ".32"); test(pattern, ".3234"); test(pattern, ".32.3"); test(pattern, ".32.33"); test(pattern, ".32.334"); } public static void test(String pattern, String value) { Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(value); boolean ok = m.matches(); System.out.println(pattern+", "+value+", "+(ok?"O":"X")); } }