匹配第一次出现的字符串的正则表达式匹配最后一个

时间:2016-05-05 19:42:28

标签: java regex

我有以下列表

Acid
stuff
goo
nasty
Probable
Acid
more stuff
Probable
Acid 
fff
ggg
Probable

我希望匹配Acid和Probable之间的所有内容。但是我的正则表达式只匹配最后一个匹配(Acid,fff,ggg,Probable)而不是第一个匹配(Acid,stuff, goo, nasty, Probable

通话类:

    public static void main(String[] args) throws IOException {


       PDFManager pdfManager = new PDFManager();
       pdfManager.setFilePath("MyFile.pdf");
       String s=pdfManager.ToText();


       if(s.contains("Thresholds")){

              BravoaltDoc_ExtractionNonDays Sum = new BravoaltDoc_ExtractionNonDays(s);
              Sum.ExtractSumNew(s);


   public class BravoaltDoc_ExtractionNonDays {
    String doc;
}}

    ArrayList<String> Day_arr = new ArrayList<String>();
    ArrayList<List<String>> Day_table2d = new ArrayList<List<String>>();
    String [] seTab3Landmarks=null;

    public BravoaltDoc_ExtractionNonDays(String doc) {
        this.doc=doc;
    }

    public String ExtractSumNew(String doc) {
        Pattern Tab3Landmarks_pattern = Pattern.compile("Acid?(.*?)Probable",Pattern.DOTALL);
        Matcher matcherTab3Landmarks_pattern = Tab3Landmarks_pattern.matcher(doc);
        while (matcherTab3Landmarks_pattern.find()) {
            doc=matcherTab3Landmarks_pattern.group(1);
            seTab3Landmarks=matcherTab3Landmarks_pattern.group(1).split("\\n|\\r");
        }
        for (String n:seTab3Landmarks){
            System.out.println(n);
        }
return docSlim;

    }

}

2 个答案:

答案 0 :(得分:2)

描述

此正则表达式将执行以下操作:

  • 将以Acid开头的子字符串与Probable
  • 匹配
  • 要求AcidProbable在他们自己的行上。如果它们嵌入在gooProbablegoo之类的字符串中间,则这些字符串将不匹配

对于这个正则表达式,我使用Case Insenstive标志,Dot匹配新行Flag。

(?:\r|\n|\A)\s*Acid\s*?[\r\n].*?[\r\n]\s*Probable\s*?(?:\r|\n|\Z)

Regular expression visualization

实施例

示例文字

注意:第三行中的困难边缘情况。

Acid
stuff
gooProbablegoo
nasty
Probable
Acid
more stuff
Probable
Acid
fff
ggg
Probable

<强>匹配

[0][0] = Acid
stuff
gooProbablegoo
nasty
Probable

[1][0] = 
Acid
more stuff
Probable

[2][0] = 
Acid
fff
ggg
Probable

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \r                       '\r' (carriage return)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \n                       '\n' (newline)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \A                       the beginning of the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  Acid                     'Acid'
----------------------------------------------------------------------
  \s*?                     whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the least amount
                           possible))
----------------------------------------------------------------------
  [\r\n]                   any character of: '\r' (carriage return),
                           '\n' (newline)
----------------------------------------------------------------------
  .*?                      any character (0 or more times (matching
                           the least amount possible))
----------------------------------------------------------------------
  [\r\n]                   any character of: '\r' (carriage return),
                           '\n' (newline)
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  Probable                 'Probable'
----------------------------------------------------------------------
  \s*?                     whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the least amount
                           possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \r                       '\r' (carriage return)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \n                       '\n' (newline)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \Z                       before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping

答案 1 :(得分:1)

您的代码正确找到所有匹配项。但是,由于每个查找都会重新分配seTab3Landmarks,因此您只会在最后打印出最后一个匹配。

如果你只想要第一场比赛,你应该使用&#34; if&#34;阻止而不是&#34;而#34; block(找到所有匹配项)。