String a ="5<w<10"; //也有可能是   ≤   
     a.replaceAll("w", "6");//w替换为6
如果在w在5-10范围返回true,否则返回false
如何实现?谢谢帮帮!

解决方案 »

  1.   

    数据库读出来是string型号的,肯定是有原因的我才这样做!
      

  2.   


    public static void main(String args[]) {
    String num = "10";
    String a = "5<w≤10";
    System.out.println(check(a, num));

    num = "5";
    a = "-2<    w       ≤ 3";
    System.out.println(check(a, num));
    }

    public static boolean check(String a, String num){
    boolean pass = true;
    String reg = "^(-?\\d+)([^\\d-]+)(-?\\d+)([^\\d-]+)(-?\\d+)$";
    a = a.replaceAll("\\s", "").replaceAll("w", num);

    Matcher macther = Pattern.compile(reg).matcher(a);
    if(macther.find()){
    if(macther.group(2).equals("<")){
    pass = pass && (Integer.parseInt(macther.group(1)) < Integer.parseInt(macther.group(3)));
    }else if(macther.group(2).equals("≤")){
    pass = pass && (Integer.parseInt(macther.group(1)) <= Integer.parseInt(macther.group(3)));
    }

    if(macther.group(4).equals("<")){
    pass = pass && (Integer.parseInt(macther.group(3)) < Integer.parseInt(macther.group(5)));
    }else if(macther.group(4).equals("≤")){
    pass = pass && (Integer.parseInt(macther.group(3)) <= Integer.parseInt(macther.group(5)));
    }
    }else{
    pass = false;
    }

    return pass;
    }
      

  3.   

    请问如下是什么意思`谢谢`你的这个可以用`
     String reg = "^(-?\\d+)([^\\d-]+)(-?\\d+)([^\\d-]+)(-?\\d+)$";
      

  4.   

    double的不可以的`public static void main(String[] args){
      String num = "51.0";
      String a = "50.00<w≤1000";
      System.out.println(check(a,num)); }
      

  5.   

    考虑了小数: public static void main(String args[]) {
    String num = "10";
    String a = "5<w≤10";
    System.out.println(check(a, num));
    System.out.println();

    num = "5";
    a = "-2<    w       ≤ 3";
    System.out.println(check(a, num));
    System.out.println();

    num = "5";
    a = "4.9<w≤10.123";
    System.out.println(check(a, num));
    System.out.println();

    }

    public static boolean check(String a, String num){
    boolean pass = true;
    String reg = "^(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)$";
    a = a.replaceAll("\\s", "").replaceAll("w", num);

    Matcher macther = Pattern.compile(reg).matcher(a);
    if(macther.find()){
    if(macther.group(3).equals("<")){
    pass = pass && (Double.parseDouble(macther.group(1)) < Double.parseDouble(macther.group(4)));
    }else if(macther.group(3).equals("≤")){
    pass = pass && (Double.parseDouble(macther.group(1)) <= Double.parseDouble(macther.group(4)));
    }

    if(macther.group(6).equals("<")){
    pass = pass && (Double.parseDouble(macther.group(4)) < Double.parseDouble(macther.group(7)));
    }else if(macther.group(6).equals("≤")){
    pass = pass && (Double.parseDouble(macther.group(4)) <= Double.parseDouble(macther.group(7)));
    }
    }else{
    pass = false;
    }

    return pass;
    }
      

  6.   

    正则表达式:
    "^(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)$"^ 字符串开始
    $ 字符串结束
    (-?\\d+(\\.\\d+)?) 匹配数字(包括小数)
    ([^\\d-\\.]+) 匹配不是以下3种符号的内容:所有数字 - . 匹配结束后,用 group(x) 把值找出来进行比较
      

  7.   


    package test5;import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test3 {
      private static final Pattern p = Pattern.compile("\\s*(\\S+)\\s*([<≤]).*?([<≤])\\s*(\\S+)\\s*");  public static void main(String[] arg) {
        String a = "5e-10 < w ≤ 6e4"; //≤
        System.out.println(new Test3().check(a, 100.201)); // true
        System.out.println(new Test3().check(a, -100.201)); // false
      }  public boolean check(String express, double value) {
        Matcher m = p.matcher(express);
        if (!m.matches()) {
          throw new IllegalArgumentException("表达式格式不正确");
        }
        double min = 0.0d;
        double max = 0.0d;
        try {
          min = Double.parseDouble(m.group(1));
          max = Double.parseDouble(m.group(4));
        } catch (NumberFormatException e) {
          throw new IllegalArgumentException("数据格式不对");
        }
        if ("<".equals(m.group(2))) {
          if ("<".equals(m.group(3))) {
            return (min < value) && (value < max);
          } else {
            return (min < value) && (value <= max);
          }
        } else {
          if ("<".equals(m.group(3))) {
            return (min <= value) && (value < max);
          } else {
            return (min <= value) && (value <= max);
          }
        }
      }
    }
      

  8.   

    先把<和<=都replace成“,”然后根据“,”split,得到3个字符串再parse字符串取到3个数字a,b,c,最后在进行判断。如果出现a==b或b==c,那再回头去判断<和<=