a1234567ab我想匹配以a开头,b结尾的最短的,就是要取其中的ab但用(?<=a).*?(?=b)只能找到a1234567ab怎样才能找到ab?谢谢!

解决方案 »

  1.   

    只用正则ms不行,因为正则表达式有另一条规则,比懒惰/贪婪规则的优先级更高:最先开始的匹配拥有最高的优先权
    它肯定是先从第一个a开始匹配的。
    可以和javaAPI(java.util.regex )配合起来,查到一个匹配的字符串后,在这个字符串里再次匹配一次或多次(就是递归操作),这样可以获得最短的匹配字符串 
      

  2.   

    代码如下:
    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class UtilTest {

    public static void main(String[] args) {
    String regEx = "a*b";
        String str="a1234567ab ";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(str);
    StringBuffer sb = new StringBuffer();
    while(m.find()){
    sb.append(m.group());
    }
    System.out.println(sb);
    }
    }/output:
    ab
      

  3.   

    可以限定一个范围:
    import java.util.regex.*;
    public class MyRegex {
    public static void main(String[] args) {
    String str="a1234567ab ";
    Matcher m=Pattern.compile("a(.{0,5})b").matcher(str);
    while(m.find())
    System.out.println(m.group());
    }
    }
    不知道这是不是你的意思
    如果单单找出ab,那么下面不行吗?
    import java.util.regex.*;
    public class MyRegex {
    public static void main(String[] args) {
    String str="a1234567ab ";
    Matcher m=Pattern.compile("ab").matcher(str);
    while(m.find())
    System.out.println(m.group());
    }
    }
      

  4.   

    a1234b567ab  
    二楼的意思是  光用正则表达式只能解决取 a1234b  不取a1234b567ab的问题。
    加上点 java 代码应该可以解决这个问题。 public static void getShortest(){
        String str ="a1234b567ab";
        String result = str;
        Pattern p = Pattern.compile("a.*?b", Pattern.MULTILINE);
        Matcher m=p.matcher(str);
        while(m.find()){
            System.out.println(m.group()); 
            if(result.length() > m.group().length()) {
                result = m.group();
            }
        }
        
        System.out.println("the shortest is :" + result); 
    }
    可以肯定的是: 上面的代码绝对不是正确答案; a.*?b 并不能匹配出所有的情况来; 楼主给的正则表达式我又实在没跑起来。。  就先用这个代替了。
      

  5.   

    懒惰好像没什么用 
    ((?<=a).*[^a](?=b))这样吧
      

  6.   


    这个不行String str="a1234567aab";就出aab了。
      

  7.   

    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test7 {
        
        public static void main(String[] args) {     String temp = "a[.[^a]]*b";
         String string="a1234b567aab";
            Pattern p = Pattern.compile(temp);
            Matcher m = p.matcher(string);
            StringBuffer stringBuffer = new StringBuffer();
          while(m.find()){
              stringBuffer.append(m.group());
          }
          System.out.println(stringBuffer);
        }
    }out:a1234bab
      

  8.   

    看看这个吧
    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class UtilTest {
        
        public static void main(String[] args) {
            String regEx = "a*b";
            String str="a1234567ab ";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(str);
            StringBuffer sb = new StringBuffer();
            while(m.find()){
                sb.append(m.group());
            }
            System.out.println(sb);
        }
    }
      

  9.   

    恩,我的意思是找夹在a和b中间最短的。
    我自己改了改,现在的不能找到最短,但已经满足我的要求了:String before = "a";
    String after = "b";
    Pattern p=Pattern.compile("(?<=" + before + ").*?(?=" + after + ")");
    Matcher m=p.matcher("a1234567a89b");
    if ( m.find() )
    {
    int start = m.start();
    int end = m.end(); Pattern pp = Pattern.compile(before);
    Matcher mm = pp.matcher(this.content.substring(0, end));
    while (mm.find())
    {
    start = mm.end();
    }
    this.value = this.content.substring(start, end);
    }
      

  10.   

    a[^a]*?b
    这样应该就可以了吧
    在ab之间不允许在出现新的a