-1

我在文件中有以下句子的开始和结束索引。

Begin:1583 End:1631
Begin:2284 End:2324 

现在我必须通过读取特定文件来检查这些索引是否存在于有效的句子中。我尝试过使用 BreakIterator。但我不明白如何检查上述索引是否存在于有效的句子中。

我试过的代码是

public class Breakiterator {

public static void main(String[] args) {

    BufferedReader br = null;
    String sCurrentLine = null;
    String l=null;
    try {
        br = new BufferedReader(new FileReader("C:/test.txt "));
        while ((sCurrentLine = br.readLine()) != null) {
            BreakIterator i=BreakIterator.getSentenceInstance(Locale.GERMANY);
            i.setText(sCurrentLine);
            for(int s=i.first(), e=i.next(); e>=0; s=e, e= i.next())
            {
              System.out.println("Sentence: from "+s+" to "+e+" \""+sCurrentLine.substring(s, e)+'"');
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


}

}

4

1 回答 1

0

尝试这个。根据您的要求修改您的正则表达式

 public static final String VALID_LINE_REG_EXP = "\\w+:\\d+.\\w+:\\d+";
    public static void main(String args[]) {
        BufferedReader br = null;
        String sCurrentLine = null;
        String l = null;
        boolean isValid;
        try {
            br = new BufferedReader(new FileReader("D:/Shamse.txt "));
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine.trim().matches(VALID_LINE_REG_EXP));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

我已经编写了这段代码,假设您的文件将包含值,TEXT:NUMBER TEXT:NUMBER您可以根据您的要求修改此正则表达式

于 2013-11-07T11:36:27.890 回答