我有一个应该捕获大写单词的正则表达式。因此,如果有一个或多个单词都是大写的,则正则表达式会找到它。但我还有另一个正则表达式,它捕获一个单词全部大写的单词。出于某种原因,第一个 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);
}
}
}
}
}