2016-09-26 84 views
3

我有這樣的文字:提取docIDs並從文件的文件並把它們放在一個HashMap

.I 1 
 
.T 
 
experimental investigation of the aerodynamics of a 
 
wing in a slipstream . 
 
.A 
 
brenckman,m. 
 
.B 
 
j. ae. scs. 25, 1958, 324. 
 
.W 
 
experimental investigation of the aerodynamics of a 
 
wing in a slipstream . 
 
    an empirical evaluation of the destalling effects was made for 
 
the specific configuration of the experiment . 
 
.I 2 
 
.T 
 
simple shear flow past a flat plate in an incompressible fluid of small 
 
viscosity . 
 
.A 
 
ting-yili 
 
.B 
 
department of aeronautical engineering, rensselaer polytechnic 
 
institute 
 
troy, n.y. 
 
.W 
 
simple shear flow past a flat plate in an incompressible fluid of small 
 
viscosity .the discussion here is restricted to two-dimensional incompressible steady flow . 
 
.I 3 
 
.T 
 
the boundary layer in simple shear flow past a flat plate . 
 
.A 
 
m. b. glauert 
 
.B 
 
department of mathematics, university of manchester, manchester, 
 
england 
 
.W 
 
the boundary layer in simple shear flow past a flat plate . 
 
the boundary-layer equations are presented for steady 
 
flow with no pressure gradient .

我需要一個正則表達式在Java中,這將給如下: 每當GET一個「.I 1」,將給出以「.W」結尾之前的文本。「I 2」

+0

好,現在的問題是:什麼樣的圖案你試過了嗎? –

+0

使用Java,您需要打開MULTILINE模式https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#MULTILINE 然後,像\ .I之類的東西\ s1。*?\。W(。*?)\。我\ s2應該工作(需要一些轉義)。如果我之後的數字對您很重要,您可能需要添加更多組。或者,由於您匹配的最後一件事似乎是您想要匹配的下一件事的一部分,您可能希望排除它。我傾向於爲這類東西編寫單元測試,然後調整正則表達式直到它工作。也許你可以發佈一些代碼來說明你確切需要什麼? –

回答

1

我認爲最簡單的方法是使用以下模式找到第一個匹配:

(?<=\.I\s1\s)[\W\w]+(?=\.I\s2) 

你會得到第一個匹配:

(?<=\.W\s)[\W\w]+ 

你會得到一個結果:

.T 
experimental investigation of the aerodynamics of a 
wing in a slipstream . 
.A 
brenckman,m. 
.B 
j. ae. scs. 25, 1958, 324. 
.W 
experimental investigation of the aerodynamics of a 
wing in a slipstream . 
    an empirical evaluation of the destalling effects was made for 
the specific configuration of the experiment . 

然後通過以下方式找到從第一場比賽的第二場比賽

experimental investigation of the aerodynamics of a 
wing in a slipstream . 
    an empirical evaluation of the destalling effects was made for 
the specific configuration of the experiment . 

你的情況可能是這樣的:

public static void main(String[] args) { 
    Map<String, String> hashMap = new HashMap<>(); 

    String text = " ... "; // your text here 

    String p1 = null, p2 = "(?<=\\.W\\s)[\\W\\w]+"; 
    Pattern r1 = null, r2 = null; 
    Matcher m1 = null, m2 = null; 

    int i = 1; 
    do { 
     if(i == 3) { 
      p1 = "(?<=\\.I\\s"+ i +"\\s)[\\W\\w]+(?=($))"; 
      i++; 
     } else 
      p1 = "(?<=\\.I\\s"+ i +"\\s)[\\W\\w]+(?=(\\.I\\s"+ ++i +"))"; 

     r1 = Pattern.compile(p1); 
     r2 = Pattern.compile(p2); 

     m1 = r1.matcher(text); 

     String textPart; 
     if(m1.find()) { 
      textPart = m1.group(0); 
      m2 = r2.matcher(textPart); 
      if(m2.find()) 
       hashMap.put(".I " + (i - 1), m2.group(0));    
     }  
    } while(i < 4); 

    for(Map.Entry<String, String> item : hashMap.entrySet()) { 
     System.out.println(item.getKey()); 
     System.out.println(item.getValue()); 
     System.out.println(); 
    } 
} 

結果:

.I 2 
simple shear flow past a flat plate in an incompressible fluid of small 
viscosity .the discussion here is restricted to two-dimensional incompressible steady flow . 


.I 1 
experimental investigation of the aerodynamics of a 
wing in a slipstream . 
    an empirical evaluation of the destalling effects was made for 
the specific configuration of the experiment . 


.I 3 
the boundary layer in simple shear flow past a flat plate . 
the boundary-layer equations are presented for steady 
flow with no pressure gradient . 
+0

謝謝你阿列克謝。它工作。 – user3701435

+0

不客氣! –

相關問題