我試圖從文本文件中提取包含2個單詞的句子。我使用了正則表達式,如下面的代碼所示。從java中的文本文件中提取包含2個單詞的句子
File doc = new File("D:\\MyFile.txt");
BufferedReader br = null;
System.out.println("enter the regex pattern to be matched");
Scanner keyboard = new Scanner(System.in);
String regxpat = keyboard.nextLine();
String line;
br = new BufferedReader(new FileReader(doc));
Pattern p = Pattern.compile(regxpat, CASE_INSENSITIVE);
while ((line = br.readLine()) != null)
{
try
{
Matcher m = p.matcher(line);
m.find();
System.out.print(m.group().toString());
}
catch (IllegalStateException e)
{
}
continue;
}
//i tried regex= "(he)*([.&&[^\.]]*?)Milan(.*?)\."
如果文字是:
"...Thomas Edison is a scientist. He invented bulb. He was born in Milan, Ohio, and grew up in Port Huron, Michigan. He was the seventh and last child of Samuel Ogden Edison, Jr...."
- 我想要的句子(句子邊界句號後面的空間)的話「he'and'milan」,即第三sentnce(該命令並不重要。任何一個句子都需要這兩個詞)
- 我試過了上面的正則表達式pattrn和其他很多
- 但它提取'米蘭'後的句子部分或2個句子從第一個'他
- 請建議得到這個任務使用正則表達式或Java中的任何其他方法
(我是2個實體之間的提取關係模式的工作做了一個方法:在這種情況下,關係模式是「出生在「b/w實體」愛迪生「和」米蘭「。 我需要這樣的句子從衆多相關[關於「愛迪生米蘭」像谷歌在愛迪生的傳記或第500個鏈接]文本文件或網頁文件如上面做futher處理)
謝謝:)我會嘗試 – user2359384 2013-05-10 04:42:16
我試過使用字符串拆分方法,但這裏有一些問題與我給的正則表達式模式或別的東西。 – user2359384 2013-05-10 15:31:39
'文件文檔=新的文件( 「d:\\ aMyFile.txt」); BufferedReader中BR = NULL;' '字符串線;' 'BR =新的BufferedReader(新的FileReader(DOC));'' while((line = br.readLine())!= null) {(retal:line.split(「\。\ s」)) System.out.println(retval); }' – user2359384 2013-05-10 15:39:30