텍스트 파일에서 2 단어를 포함하는 문장을 추출하려고합니다. 나는 아래 코드와 같이 정규 표현식을 사용했다. 텍스트는자바의 텍스트 파일에서 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...."
- 은 내가 3 sentnce 즉 단어 'he'and'milan'(와 (문장 경계 공백 마침표이다) 문장을 원한다 순서는 중요하지 않습니다. 두 단어로 된 모든 문장이 필요합니다.)
- 나는 위의 정규 표현식과 많은 다른 단어를 시도했으나
- 문장을 '밀란'또는 2 문장의 첫 번째 ' 관계 패턴 "의 탄생이 경우 : 그는 '
- 이 작업은 정규식 또는
(나는 2 엔티티 사이의 관계 패턴을 추출하는 작업입니다 자바의 다른 방법을 사용하여 수행 얻을 수있는 방법을 제안 해주십시오 "b/w 실체"edison "과"milan ". 내가
나는 시험해 보겠습니다. – user2359384
문자열 분할 방법을 사용해 보았습니다. 그러나 여기에 정규 표현식 패턴과 관련된 문제가 있습니다. – user2359384
'파일 문서 = 새로운 파일 ("D : \\ aMyFile.txt");의 BufferedReader BR = NULL;' '문자열 라인;' 'BR = 새의 BufferedReader (새을 FileReader (문서));' ' while (line = br.readLine())! = null) { for (문자열 retval : line.split ("\. \ s")) System.out.println (retval); } ' – user2359384