Java模式匹配器在完美处理之前的九个类似模式/匹配后返回null

Java模式匹配器在完美处理之前的九个类似模式/匹配后返回null,java,regex,matcher,Java,Regex,Matcher,此类打开一个文件,在/处进行分隔并返回一个字符串。然后,我使用带有Matcher的模式集来搜索字符串并返回数据片段。稍后,这将用于将数据重新格式化为许多文件和特定顺序。目前,这个过程已经处理了多个模式匹配,但是当我传递EditorList和AuthorList时,当数据清楚地存在时,它返回null。当程序尝试使用空字符串时崩溃,我得到一个空指针异常。这是我第一次使用模式和匹配器,我在这里忽略了什么明显的事情 import java.io.File; import java.io.FileNotF

此类打开一个文件,在
/
处进行分隔并返回一个字符串。然后,我使用带有Matcher的模式集来搜索字符串并返回数据片段。稍后,这将用于将数据重新格式化为许多文件和特定顺序。目前,这个过程已经处理了多个模式匹配,但是当我传递
EditorList
AuthorList
时,当数据清楚地存在时,它返回null。当程序尝试使用空字符串时崩溃,我得到一个空指针异常。这是我第一次使用模式和匹配器,我在这里忽略了什么明显的事情

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

public class MatchMethod {
public static String cancerCat=null;    
public static String paperType="book";
public static String Paper=null;
public static String Title=null;
public static String Abstr=null;
public static String Publi=null;
public static String Editi=null;
public static String Pagen=null;
public static String Bookt=null;
public static String Years=null;
public static String Editl=null;
public static String Edito=null;
public static String Authl=null;
public static String Autho=null;
public static String Foren=null;
public static String Initi=null;
public static String Lastn=null;

public static Scanner scanner;
public static File file;

static Pattern PaperBegin = Pattern.compile("<PaperBegin>(.+?)</PaperBegin>");
static Pattern PaperTitle = Pattern.compile("<PaperTitle>(.+?)</PaperTitle>");
static Pattern Abstract =   Pattern.compile("<Abstract>(.+?)</Abstract>");
static Pattern BookTitle =  Pattern.compile("<BookTitle>(.+?)</BookTitle>");
static Pattern Publisher =  Pattern.compile("<Publisher>(.+?)</Publisher>");
static Pattern Edition =    Pattern.compile("<Edition>(.+?)</Edition>");
static Pattern Page =       Pattern.compile("<Page>(.+?)</Page>");
static Pattern EditorList = Pattern.compile("<EditorList>(.+?)</EditorList>");
static Pattern Editor =     Pattern.compile("<Editor>(.+?)</Editor>");
static Pattern Year =       Pattern.compile("<Year>(.+?)</Year>");   
static Pattern AuthorList = Pattern.compile("<AuthorList>(.+?)</AuthorList>");
static Pattern Author =     Pattern.compile("<Author>(.+?)</Author>");
static Pattern ForeName =   Pattern.compile("<ForeName>(.+?)</ForeName>");
static Pattern Initials =   Pattern.compile("<Initials>(.+?)</Initials>");
static Pattern LastName =   Pattern.compile("<LastName>(.+?)</LastName>");

public static String find (String text, Pattern pattern)
{
    String found=null;
    Matcher match = pattern.matcher(text);
    if (match.find()) {found = match.group(1);}
    System.out.println((pattern.toString()) + " found: "+found);
    return  found;
}

@SuppressWarnings("resource")
static void readBook (String book) throws FileNotFoundException
{
    file = new File (book);
    scanner = new Scanner(file).useDelimiter("\\//");
    while (scanner.hasNext()) 
    {           
        Paper=scanner.next();
        Title = find (Paper, PaperTitle);
        Abstr = find (Paper, Abstract);
        Publi = find (Paper, Publisher);
        Editi = find (Paper, Edition);
        Pagen = find (Paper, Page);
        Bookt = find (Paper, BookTitle);
        Years = find (Paper, Year);         
        Editl = find (Paper, EditorList);
        Authl = find (Paper, AuthorList);

        Matcher mEdito = Editor.matcher(Editl);
        Edito = mEdito.group(1);
        while (mEdito.find()) // while loop to find all editors
        {
            System.out.println("Searching editors");                
            Foren = find (Edito, ForeName);
            Initi = find (Edito, Initials);
            Lastn = find (Edito, LastName);
            System.out.println ("EDITORS: " + Bookt + "\t" + Foren + "\t" + Initi + "\t" + Lastn);
        }
        Matcher mAutho = Author.matcher(Authl);
        while (mAutho.find()) // while loop to find all editors
        {
            System.out.println("Searching authors");
            Autho = mAutho.group(1);
            Foren = find (Autho, ForeName);
            Initi = find (Autho, Initials);
            Lastn = find (Autho, LastName);
            System.out.println ("AUTHORS: " + Bookt + "\t" + Foren + "\t" + Initi + "\t" + Lastn);
        }   
    }
}

public static void main(String[] args) throws IOException 
{
    readBook ("CC_book.txt"); //opens text file to be mined


    //Start reading Colon Cancer Book Information

    //Start reading Endocrine Cancer Book Information

    //Start reading Lung Cancer Book Information

    //Start reading Other Cancer Book Information   

    //Start reading Pancreatic Cancer Book Information
    scanner.close();
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.Scanner;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类匹配方法{
公共静态字符串cancerCat=null;
公共静态字符串paperType=“book”;
公共静态字符串Paper=null;
公共静态字符串Title=null;
公共静态字符串absr=null;
公共静态字符串Publi=null;
公共静态字符串Editi=null;
公共静态字符串Pagen=null;
公共静态字符串Bookt=null;
公共静态字符串Years=null;
公共静态字符串Editl=null;
公共静态字符串Edito=null;
公共静态字符串Authl=null;
公共静态字符串Autho=null;
公共静态字符串Foren=null;
公共静态字符串Initi=null;
公共静态字符串Lastn=null;
公共静态扫描仪;
公共静态文件;
静态模式PaperBegin=Pattern.compile((.+?));
静态模式PaperTitle=Pattern.compile((.+?));
静态模式抽象=Pattern.compile((.+?));
静态模式BookTitle=Pattern.compile((.+?));
静态模式发布器=Pattern.compile((.+?));
静态模式版本=Pattern.compile((.+?));
静态模式页面=Pattern.compile((.+?));
静态模式编辑器列表=Pattern.compile((.+?));
静态模式编辑器=Pattern.compile((.+?));
静态模式年份=Pattern.compile(“(.+?)”);
静态模式AuthorList=Pattern.compile((.+?));
静态模式作者=Pattern.compile((.+?));
静态模式名=Pattern.compile((.+?));
静态模式初始值=Pattern.compile((.+?));
静态模式LastName=Pattern.compile((.+?));
公共静态字符串查找(字符串文本、模式)
{
找到的字符串=null;
Matcher match=pattern.Matcher(文本);
if(match.find()){found=match.group(1);}
System.out.println((pattern.toString())+“found:”+found);
发现退货;
}
@抑制警告(“资源”)
静态void readBook(字符串book)引发FileNotFoundException
{
文件=新文件(书);
scanner=新的扫描仪(文件)。使用分隔符(\\/);
while(scanner.hasNext())
{           
纸张=扫描仪。下一步();
标题=查找(论文、论文标题);
Abstr=查找(论文、摘要);
Publi=查找(论文、出版商);
Editi=查找(论文,版本);
Pagen=查找(纸张,页面);
Bookt=查找(纸张、书名);
年=查找(论文,年);
Editl=查找(论文,编辑列表);
Authl=查找(纸张、作者列表);
Matcher mEdito=Editor.Matcher(Editl);
Edito=mEdito.group(1);
while(mEdito.find())//while循环查找所有编辑器
{
System.out.println(“搜索编辑器”);
Foren=find(Edito,ForeName);
Initi=find(Edito,缩写);
Lastn=find(Edito,LastName);
System.out.println(“编辑器:”+Bookt+“\t”+Foren+“\t”+Initi+“\t”+Lastn);
}
Matcher mAutho=Author.Matcher(Authl);
while(mAutho.find())//while循环查找所有编辑器
{
System.out.println(“搜索作者”);
Autho=mAutho.group(1);
Foren=find(Autho,ForeName);
Initi=find(Autho,缩写);
Lastn=find(Autho,LastName);
System.out.println(“作者:”+Bookt+“\t”+Foren+“\t”+Initi+“\t”+Lastn);
}   
}
}
公共静态void main(字符串[]args)引发IOException
{
readBook(“CC_book.txt”);//打开要挖掘的文本文件
//开始阅读结肠癌书籍信息
//开始阅读内分泌癌书籍信息
//开始阅读肺癌书籍信息
//开始阅读其他癌症书籍信息
//开始阅读胰腺癌书籍信息
scanner.close();
}
}

以下是文件中的示例数据:

立即和延迟乳房重建后所有并发症的真实发生率。
背景:乳房再造术后自我形象的改善和心理健康状况的改善已经得到了很好的证明。为了确定以最低发病率优化结果的方法,作者基于重建方法和时间检查了其结果和并发症。方法:作者回顾了在一个三级转诊中心,在一名外科医生的监督下,在6年期间进行的乳腺癌乳腺切除术后的所有乳房重建。回顾了重建方法和时机、患者特征和并发症发生率。结果:对240名连续妇女进行了重建(94例双侧和146例单侧;334例全部重建)。重建时间在立即(n=167)和延迟(n=167)之间平均分配。自体组织(n=192)比组织扩张器/植入物重建(n=142)更常见,游离上腹部深下穿支是最常见的游离皮瓣(n=124)。作者发现无论是立即进行还是延迟进行自体重建,其并发症发生率没有差异。然而,与对照组相比,立即放置组织扩张器后并发症发生率明显较高
static Pattern Author = Pattern.compile("(?s)<Author>(.+?)</Author>");
Consider the string: I like cats, I wonder if you like cats
"I (.*) cats" matches the whole string.
"I (.*?) cats" matches "I like cats", and if  the global flag is on, seperately matches "I wonder if you like cats"
<Author(?: [\w\-]*="[^"]*")*>(.+?)</Author>
<Author ValidYN="(?:Y|N)">(.+?)</Author>
    Matcher mEdito = Editor.matcher(Editl);
    Edito = mEdito.group(1);
    while (mEdito.find()) // while loop to find all editors
    {
    Matcher mEdito = Editor.matcher(Editl);
    while (mEdito.find()) // while loop to find all editors
    {
        Edito = mEdito.group(1);
static Pattern EditorList = Pattern.compile("(?s)<EditorList>(.+?)</EditorList>");
...
static Pattern AuthorList = Pattern.compile("(?s)<AuthorList>(.+?)</AuthorList>");