Java的正则表达式怎么验证一个数在0-493和500的数字
3条回答 默认 最新
拽拽的初行者 2010-01-26 10:18关注[code="java"]
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test {
public static String modify(int numb){ if(numb<10) return "00"+numb; else if (numb <100) return "0"+numb; else return ""+numb; } public static void isInRange(int numb){ String str = Test.modify(numb); Matcher m1 = Pattern.compile("^[0-4][0-9][0-9]$").matcher(str); Matcher m2 = Pattern.compile("^500$").matcher(str); Matcher m3 = Pattern.compile("^49[4-9]$").matcher(str); if( m1.matches()){ if(!m3.matches()){ System.out.println(str+"匹配"); } }else if(m2.matches()){ System.out.println(str+"匹配"); } } public static void main(String[] args) { Test.isInRange(2); Test.isInRange(23); Test.isInRange(279); Test.isInRange(499); Test.isInRange(500); Test.isInRange(502); }}
[/code]
[b][color=blue]你试试[/color][/b]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报