日期:2014-05-20 浏览次数:20930 次
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());
}
}
------解决方案--------------------
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).*[^a](?=b))这样吧