我试图从文本文件中提取包含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