日期:2014-05-20  浏览次数:20678 次

求正则表达式问题解答
FileReader reader = new FileReader("f://file.TextGrid"); 
  BufferedReader br = new BufferedReader(reader); 
  Pattern sizePattern = Pattern.compile("(^size)+(.)+[0-9]+"); 
  Pattern xmPattern = Pattern.compile("(xm)(.)+(\\d+\\.)?\\d+"); 
  String strText; 
  int lineIndex = 0; 
  while ((strText = br.readLine()) != null) { 
  Matcher sizeMatcher = sizePattern.matcher(strText); 
  Matcher xmMatcher = xmPattern.matcher(strText); 
  while(xmMatcher.find()){ 
  if(xmMatcher.group() != null) { 
  for(int j = 0; j < xmMatcher.groupCount(); j++) 
  System.out.println(xmMatcher.group(j)); 
  } 
  } 
  } 
   
   
  br.close(); 
  reader.close(); 
输出结果是: 
xmin = 0.000000 
xm 

只想要第一行,请问错误在那?估计是groupCount的问题,请指教,谢谢 


------解决方案--------------------
Java code

只要第一行
System.out.println(xmMatcher.group(0));
给j变成0了
一般group(0)代表整体匹配到的!