0
Scanner scanner = new Scanner(new File(pathf2));
         scanner.useDelimiter("########");
String pattern2="^[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(]";  

Pattern r2 = Pattern.compile(pattern2);
Matcher m2 = r2.matcher(line);


 while (m2.find()) 
 {
     rel = m2.group();
     rel = rel.substring(0, rel.length()-1).trim();                 
     System.out.println("The relation are " + rel); 
 }

这是我的输入文件....我需要一个匹配块中每个句子中的 3 个字母的正则表达式.. 上面的编码只返回与块中第一行匹配的“agt”。然后它返回“obj”是第二个块的开始。我需要一个块中每行的 3 个字符....如果我在正则表达式中删除“^”,它也需要句子中的第二个单词....请帮助

agt(eat(icl>consume>do,agt>living_thing,obj>concrete_thing,ins>thing).@entry.@present,ram(icl>volatile_storage>thing,equ>random-access_memory))
obj(eat(icl>consume>do,agt>living_thing,obj>concrete_thing,ins>thing).@entry.@present,rice(icl>grain>thing))

########

obj(clear(icl>remove>do,plf>thing,obj>thing,ins>thing).@entry.@past,bloodsworth.@topic)
man(clear(icl>remove>do,plf>thing,obj>thing,ins>thing).@entry.@past,ultimately(icl>how,com>ultimate))
ins(clear(icl>remove>do,plf>thing,obj>thing,ins>thing).@entry.@past,evidence(icl>indication>thing))
obj(gather(icl>do,equ>accumulate,plf>thing,agt>thing,obj>thing,plc>thing).@state,evidence(icl>indication>thing))
plf(gather(icl>do,equ>accumulate,plf>thing,agt>thing,obj>thing,plc>thing).@state,from)
mod(stain(icl>appearance>thing).@indef,semen(icl>liquid_body_substance>thing))
obj(from,stain(icl>appearance>thing).@indef)
plc(gather(icl>do,equ>accumulate,plf>thing,agt>thing,obj>thing,plc>thing).@state,panties.@pl)
pos(panties.@pl,victim(icl>unfortunate>thing).@def)
 ########
4

3 回答 3

0

将模式更改为:

String pattern2="(?m)^[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(]";  
于 2013-10-11T10:28:15.350 回答
0

我认为您需要循环获取每一行,直到达到 EOF

试试下面的代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(pathf2));
        scanner.useDelimiter("########");
        String pattern2 = "^[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(]";
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            Pattern r2 = Pattern.compile(pattern2);
            Matcher m2 = r2.matcher(line);
            while (m2.find()) {
                String rel = m2.group();
                rel = rel.substring(0, rel.length() - 1).trim();
                System.out.println("The relation are " + rel);
            }
        }
        scanner.close();

    }

}
于 2013-10-11T09:19:05.097 回答
0

You can split each block into lines and check each line separately:

    Scanner scanner = new Scanner(new File("/tmp/x"));
    scanner.useDelimiter("########");

    String pattern2="(^[a-z]{3})";  
    Pattern r2 = Pattern.compile(pattern2);     

    while (scanner.hasNext()) {
        System.out.println( "### NEXT BLOCK ###" );

        String block = scanner.next();

        String [] lines = block.split("\n");

        for(String line: lines ) {
            Matcher m2 = r2.matcher(line);
            if( m2.find() ) {
                System.out.println( "\t" + m2.group() );    
            }
        }
    }
    scanner.close();
于 2013-10-11T09:26:53.383 回答