a1234567ab我想匹配以a开头,b结尾的最短的,就是要取其中的ab但用(?<=a).*?(?=b)只能找到a1234567ab怎样才能找到ab?谢谢!
解决方案 »
- java显示问题求教
- eclipse 中 spring 如何导入?
- 如word一样菜单下边的一排图标,在JFrame中能够加入吗?谢谢了。
- 将字符串里:< 替换成< > 替换成> & 替换成& ' 替换成'" 替换成"用什么样的正则呢?
- 小妹请教怎样把一个50*50象素的矢量图形转化为24*24象素的,并且怎样才能保存到数据库中去?
- eclipse的编译路径问题,请高手指点
- 面向对象思想(OOP)
- 急!100分!用applet做一个画流程图的界面,有几个疑问?立马给分!
- Non-Blocking SocketChannel的问题
- 关于线程的问题
- java中的方法trim()
- 求解swing问题大鸟来9
它肯定是先从第一个a开始匹配的。
可以和javaAPI(java.util.regex )配合起来,查到一个匹配的字符串后,在这个字符串里再次匹配一次或多次(就是递归操作),这样可以获得最短的匹配字符串
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
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());
}
}
二楼的意思是 光用正则表达式只能解决取 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 并不能匹配出所有的情况来; 楼主给的正则表达式我又实在没跑起来。。 就先用这个代替了。
((?<=a).*[^a](?=b))这样吧
这个不行String str="a1234567aab";就出aab了。
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
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);
}
}
我自己改了改,现在的不能找到最短,但已经满足我的要求了: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);
}
这样应该就可以了吧
在ab之间不允许在出现新的a