Java 从文件中提取docid和文档,并将它们放在hashmap中

Java 从文件中提取docid和文档,并将它们放在hashmap中,java,regex,file,hashmap,Java,Regex,File,Hashmap,我有这样一个文本: public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); String text = " ... "; // your text here String p1 = null, p2 = "(?<=\\.W\\s)[\\W\\w]+"; Pattern r1 = null, r2 = null;

我有这样一个文本:

public static void main(String[] args) {
    Map<String, String> hashMap = new HashMap<>();

    String text = " ... ";  // your text here

    String p1 = null, p2 = "(?<=\\.W\\s)[\\W\\w]+";
    Pattern r1 = null, r2 = null;
    Matcher m1 = null, m2 = null;

    int i = 1;
    do {
        if(i == 3) {
            p1 = "(?<=\\.I\\s"+ i +"\\s)[\\W\\w]+(?=($))";
            i++;
        } else 
            p1 = "(?<=\\.I\\s"+ i +"\\s)[\\W\\w]+(?=(\\.I\\s"+ ++i +"))";  

        r1 = Pattern.compile(p1);
        r2 = Pattern.compile(p2);

        m1 = r1.matcher(text);

        String textPart;
        if(m1.find()) {
            textPart = m1.group(0);
            m2 = r2.matcher(textPart);
            if(m2.find()) 
                hashMap.put(".I " + (i - 1), m2.group(0));              
        }    
    } while(i < 4);

    for(Map.Entry<String, String> item : hashMap.entrySet()) {
        System.out.println(item.getKey());
        System.out.println(item.getValue());
        System.out.println();
    }
}
.I 1
T
飞机空气动力学实验研究
滑流中的机翼。
A.
布伦克曼,m。
B
Jae。scs。25, 1958, 324.
W
飞机空气动力学实验研究
滑流中的机翼。
对这两种材料的脱层效果进行了实证评估
实验的具体配置。
.I.2
T
小型不可压缩流体中平板的简单剪切流
粘度
A.
丁伊利
B
伦斯勒理工学院航空工程系
研究所
纽约州特洛伊。
W
小型不可压缩流体中平板的简单剪切流
这里的讨论仅限于二维不可压缩定常流。
.I.3
T
通过平板的简单剪切流中的边界层。
A.
MB格劳特
B
曼彻斯特大学数学系,曼彻斯特
英格兰
W
通过平板的简单剪切流中的边界层。
给出了定常边界层方程

无压力梯度流动。
我认为最简单的方法是使用以下模式找到第一个匹配项:

(?<=\.I\s1\s)[\W\w]+(?=\.I\s2)
(?<=\.W\s)[\W\w]+
然后使用以下模式从第一个匹配中查找第二个匹配:

(?<=\.I\s1\s)[\W\w]+(?=\.I\s2)
(?<=\.W\s)[\W\w]+

在您的情况下,可能是这样的:

public static void main(String[] args) {
    Map<String, String> hashMap = new HashMap<>();

    String text = " ... ";  // your text here

    String p1 = null, p2 = "(?<=\\.W\\s)[\\W\\w]+";
    Pattern r1 = null, r2 = null;
    Matcher m1 = null, m2 = null;

    int i = 1;
    do {
        if(i == 3) {
            p1 = "(?<=\\.I\\s"+ i +"\\s)[\\W\\w]+(?=($))";
            i++;
        } else 
            p1 = "(?<=\\.I\\s"+ i +"\\s)[\\W\\w]+(?=(\\.I\\s"+ ++i +"))";  

        r1 = Pattern.compile(p1);
        r2 = Pattern.compile(p2);

        m1 = r1.matcher(text);

        String textPart;
        if(m1.find()) {
            textPart = m1.group(0);
            m2 = r2.matcher(textPart);
            if(m2.find()) 
                hashMap.put(".I " + (i - 1), m2.group(0));              
        }    
    } while(i < 4);

    for(Map.Entry<String, String> item : hashMap.entrySet()) {
        System.out.println(item.getKey());
        System.out.println(item.getValue());
        System.out.println();
    }
}

好的,现在的问题是:您已经尝试了什么模式?使用Java,您需要在之后打开多行模式,类似\.I\s1.*?\.W(.*?\.I\s2)的东西应该可以工作(需要一些转义)。如果I后面的数字对您很重要,您可能需要添加更多组。或者,由于最后匹配的内容似乎是下一个要匹配的内容的一部分,因此您可能希望将其排除在外。我倾向于为这类东西编写一个单元测试,然后调整正则表达式直到它起作用。也许你可以发布一些代码来说明你到底需要什么?