[英]Regex to match first occurrence of a string is matching the last
我有以下清單
Acid
stuff
goo
nasty
Probable
Acid
more stuff
Probable
Acid
fff
ggg
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;
}
}
此正則表達式將執行以下操作:
Acid
開頭的子字符串匹配為Probable
Acid
和Probable
在自己的行。 如果它們嵌入在像gooProbablegoo
這樣的字符串中間, gooProbablegoo
它們將不匹配 對於此正則表達式,我使用了Case Insenstive標志,而Dot匹配了新行Flag。
(?:\r|\n|\A)\s*Acid\s*?[\r\n].*?[\r\n]\s*Probable\s*?(?:\r|\n|\Z)
示范文本
注意:第三行中的困難邊緣情況。
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
您的代碼正確找到所有匹配項。 但是,由於每個查找都重新分配了seTab3Landmarks
,因此您只會在末尾打印出最后一個匹配項。
如果只希望第一個匹配,則應使用“ if”塊而不是“ while”塊(可找到所有匹配項)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.