java中怎样从字符串中获取以a开头b结尾的子字符串?

如题所述

import java.util.regex.*;
public class Test {
public static void main(String[] args){
String str = "addcjbjacffbcd";
String reg = "a[^a]*b";    
//利用正则表达式定义规则,a开头中间除了a任意都获取,b结尾
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-01-04
很简单的.正则表达式...给个例子,我帮你写..
先看看这个吧.不行的话在告诉我.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
public static void main(String[] args){
String s = "a你想要的数据b";
Pattern p = Pattern.compile("a(.*?)b");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println(m.group(1));
}
}
}
运行结果:你想要的数据本回答被网友采纳
第2个回答  2011-12-30
public class test {
public static void main(String[] args) {
String s ="a你要的内容bcd";
s=s.substring(s.lastIndexOf("a")+1,s.lastIndexOf("b"));
System.out.println(s);
}
}
第3个回答  2011-12-30
用正则表达式追问

请举例说明吧?

相似回答