import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.MatchResult; public static void main (String args[]) {
String seesee = "fasfsda dfafds 6.56 TAB_Edison Column 6.58 ABC_Tomas Column ";
// 搜索以数字+.+数字+空格+字母+_+空格 的字符串
String regex = "\\d+\\.\\d+\\s+\\w+\\s+";
try{
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern = compiler.compile(regex);
PatternMatcher matcher = new Perl5Matcher();
if(matcher.contains(seesee,pattern)){
MatchResult result = matcher.getMatch();
System.out.println(result);
}
}
catch(Exception e){
e.printStackTrace();
}
}
照道理应该打印出6.56 TAB_Edison Column 6.58 ABC_Tomas Column,但现在只能打印出6.56 TAB_Edison Column ,如果把6.56 TAB_Edison Column 从String seesee中去掉,6.58 ABC_Tomas Column就能打印出来,这是为什么?我怎么才能把两个同时打印出来?求高手解答。
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.MatchResult; public static void main (String args[]) {
String seesee = "fasfsda dfafds 6.56 TAB_Edison Column 6.58 ABC_Tomas Column ";
// 搜索以数字+.+数字+空格+字母+_+空格 的字符串
String regex = "\\d+\\.\\d+\\s+\\w+\\s+";
try{
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern = compiler.compile(regex);
PatternMatcher matcher = new Perl5Matcher();
if(matcher.contains(seesee,pattern)){
MatchResult result = matcher.getMatch();
System.out.println(result);
}
}
catch(Exception e){
e.printStackTrace();
}
}
照道理应该打印出6.56 TAB_Edison Column 6.58 ABC_Tomas Column,但现在只能打印出6.56 TAB_Edison Column ,如果把6.56 TAB_Edison Column 从String seesee中去掉,6.58 ABC_Tomas Column就能打印出来,这是为什么?我怎么才能把两个同时打印出来?求高手解答。
试试
只要找到了匹配的字符串就停止再寻找了,所以后面那个就被它丢弃了
import java.util.regex.Pattern;public class Test {
public static void main(String[] args) throws java.text.ParseException {
String regex = "\\d+\\.\\d+\\s+\\w+\\s+ ";
String input= "fasfsda dfafds 6.56 TAB_Edison Column 6.58 ABC_Tomas Column "; Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(m.group());
}
}
}/*
6.56 TAB_Edison
6.58 ABC_Tomas
*/
结帖了
有人解决了,代码如下import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test {
public static void main(String[] args) throws java.text.ParseException {
String regex = "\\d+\\.\\d+\\s+\\w+\\s+ ";
String input= "fasfsda dfafds 6.56 TAB_Edison Column 6.58 ABC_Tomas Column "; Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(m.group());
}
}
}/*
6.56 TAB_Edison
6.58 ABC_Tomas
*/
结帖了