Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一行上多个标记的提取_Java_Regex - Fatal编程技术网

Java 一行上多个标记的提取

Java 一行上多个标记的提取,java,regex,Java,Regex,我会尽量解释清楚的。我输入一个文本,逐行读取文件(我已经逐字尝试了)。阅读后,我应用了一个有效的正则表达式,但输出文件不适合我,我得到了这种类型的输出: <pers> Sarkozy </pers> <pers> Muscat </pers> , le secrétaire général , devant <pers> Sarkozy </pers> <pers> Muscat </pers> 萨

我会尽量解释清楚的。我输入一个文本,逐行读取文件(我已经逐字尝试了)。阅读后,我应用了一个有效的正则表达式,但输出文件不适合我,我得到了这种类型的输出:

<pers> Sarkozy </pers>
<pers> Muscat </pers> , le secrétaire général , devant <pers> Sarkozy </pers>
<pers> Muscat </pers>
萨科齐 马斯喀特、勒塞塞雷泰尔、德凡·萨科齐 马斯喀特 我希望:

<pers> Sarkozy </pers>
<pers> Muscat </pers>
<pers> Sarkozy </pers>
<pers> Muscat </pers> 
萨科齐 马斯喀特 萨科齐 马斯喀特 我不明白问题出在哪里。。。我觉得从它与我的模式匹配几次的那一刻起,它需要我一行,整个行,而不仅仅是标签。。。是我的正则表达式不好,还是它是我用来读取文件的

我的代码:

public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        File file = new File(
                monfichier);
        String fileContent = readFileAsString(file.getAbsolutePath());

        countNE countne = new countNE();

        String result = countne.countNE(fileContent);
         System.out.println(result);


    }

    public String countNE(String fileContent) throws java.io.IOException {
        int i = 0;
        Hashtable<String, Integer> table = new Hashtable();
        int nbOcc = 0;

        String regPerson = "<pers>.?\\w*.?</pers>";

        Pattern pPers = Pattern.compile(regPerson);

        Matcher m = pPers.matcher(fileContent);
        String result = "";

        while (m.find()) {
            String person = m.group();
            // System.out.println(person + " " + i);
            // System.out.println(person);
            i++;
            result += person + "\n";
        }
        return result;

    }

    public static String readFileAsString(String filePath)
            throws java.io.IOException {
        String chaine = "";
        // lecture du fichier texte
        try {
            InputStream ips = new FileInputStream(filePath);
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String ligne;
            while ((ligne = br.readLine()) != null) {
                chaine += ligne + "\n";

            }
            br.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }

        System.out.println(chaine);
        return chaine;
    }

}
publicstaticvoidmain(字符串[]args)引发IOException{
//TODO自动生成的方法存根
文件=新文件(
蒙菲希尔);
字符串fileContent=readFileAsString(file.getAbsolutePath());
countNE countNE=新countNE();
字符串结果=countne.countne(文件内容);
系统输出打印项次(结果);
}
publicstringcountne(stringfilecontent)抛出java.io.IOException{
int i=0;
Hashtable=新的Hashtable();
int-nbOcc=0;
字符串regPerson=“.?\\w*”;
Pattern-pPers=Pattern.compile(regPerson);
Matcher m=pPers.Matcher(fileContent);
字符串结果=”;
while(m.find()){
字符串person=m.group();
//System.out.println(person+“”+i);
//系统输出打印项次(人);
i++;
结果+=人+“\n”;
}
返回结果;
}
公共静态字符串readFileAsString(字符串文件路径)
抛出java.io.IOException{
字符串chaine=“”;
//费希尔文本讲座
试一试{
InputStream ips=新文件InputStream(文件路径);
InputStreamReader ipsr=新的InputStreamReader(ips);
BufferedReader br=新的BufferedReader(ipsr);
弦线;
而((ligne=br.readLine())!=null){
chaine+=ligne+“\n”;
}
br.close();
}捕获(例外e){
System.out.println(例如toString());
}
System.out.println(chaine);
返回链;
}
}
谢谢你的帮助

问题在于

.? 
在您的正则表达式中。
它匹配任何内容,包括

因此,为了得到您想要的内容,您可以先将该行拆分,或者通过使用较短的字符间隔进行匹配(例如
[a-zA-Z]
或类似方式),从
中排除

正确的方法:

public static String countNE(String fileContent) throws java.io.IOException {
    Hashtable<String, Integer> table = new Hashtable();
    int nbOcc = 0;

    String regPerson = "(<pers>.*?</pers>)";
    // String regPerson = "(<.*>.*?</.*>)";

    Pattern pPers = Pattern.compile(regPerson);

    Matcher m = pPers.matcher(fileContent);
    String result = "";
    while(m.find())
    {
            result += m.group(1);
    }

    return result;
}
publicstaticstringcountne(stringfilecontent)抛出java.io.IOException{
Hashtable=新的Hashtable();
int-nbOcc=0;
字符串regPerson=“(.*?”;
//字符串regPerson=“(.*?”;
Pattern-pPers=Pattern.compile(regPerson);
Matcher m=pPers.Matcher(fileContent);
字符串结果=”;
while(m.find())
{
结果+=m组(1);
}
返回结果;
}

regex不适用于此。你考虑过嵌套标签吗?改用解析器。你能把你的代码从读取文件转换成只使用硬编码字符串吗(这样我们就有了一个容易复制的例子)?使用
StringReader
应该可以很容易地进行更改。
是一个可选字符,因此它不能匹配