0

我有一个应该捕获大写单词的正则表达式。因此,如果有一个或多个单词都是大写的,则正则表达式会找到它。但我还有另一个正则表达式,它捕获一个单词全部大写的单词。出于某种原因,第一个 reg ex 捕获了一个全大写的单词,最后有一个空格。这是我的代码。

    //This looks for All Cap Words inside parens-completed
    String ucParensRegEx = "\\([A-Z]+\\)";
    regexParser(we, ucParensRegEx);
    //This looks for All Upper case words with two or more letters.- completed
    String twoPlusUCRegEx = "[A-Z][A-Z]+";
    regexParser(we, twoPlusUCRegEx);

    String letNumRegEx = "[A-Z][A-Z0-9][A-Z]+";
    regexParser(we, letNumRegEx);

    //Looks for Uppercase words that start with a number-Completed
    String numLetRegEx = "[0-9][A-Z][A-Z]+";
    regexParser(we, numLetRegEx);

    String upperwhitespaceRegEx = "(\\b[A-Z'][A-Z]+\\b\\s*)+";
    regexParser(we, upperwhitespaceRegEx);

private void regexParser(WordExtractor we, String regex) {
    if (we.getParagraphText() != null) {
        String[] dataArray = we.getParagraphText();

        for (int i = 0; i < dataArray.length; i++) {
            String data = dataArray[i].toString();
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(data);
            while (m.find()) {
                if (!sequences.contains(data.substring(m.start(), m.end())) && !data.equals("US ") && !data.contains("ARABIC") && !data.contains("ALATEC") && !data.contains("HYPERLINK")) {
                    sequences.add(data.substring(m.start(), m.end()));
                    System.out.println(data.substring(m.start(), m.end()));
                    Acronym acc = new Acronym(data.substring(m.start(), m.end()), data, "", false);
                    newAcList.add(acc);
                }
            }
        }
    }
}
4

1 回答 1

3
"\\b[A-Z'][A-Z]+(\s+[A-Z'][A-Z]+)*\\b"

内部单词边界是不必要的(因为\s[A-Z]在空格和大写字母之间定义了单词边界)。所以你需要做的就是匹配一个大写单词,然后可选地匹配一堆其他的大写单词。

于 2012-09-18T22:30:15.883 回答