正则表达式,用于查找块中每行的前三个字符

时间:2013-10-11 09:05:15

标签: java

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)
 ########

3 个答案:

答案 0 :(得分: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();

    }

}

答案 1 :(得分:0)

您可以将每个块拆分成行并分别检查每一行:

    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();

答案 2 :(得分:0)

将模式更改为:

String pattern2="(?m)^[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(]";