我正在尝试解决这个问题:
Get document on some condition in elastic search java API
我的逻辑是,首先我们获取字符串中月份的所有位置,然后提取下一个单词,即 4 位或 2 位年份,然后使用它计算差异。
为了获得几个月的位置,我使用这段代码:-
String[] threeMonthArray=new String[]{" Jan "," Feb "," Mar "," Apr "," May "," June "," July "," Aug "," Sep "," Oct "," Nov "," Dec "};
String[] completeMonthArray=new String[]{"January","Feburary","March","April","May","June","July","Augest","September","October","November","December"};
List indexArray=new ArrayList();
for(int i=0;i<threeMonthArray.length;i++){
int index = parsedContent.toLowerCase().indexOf(threeMonthArray[i].toLowerCase());
while (index >= 0) {
System.out.println(threeMonthArray[i]+" : "+index+"------");
indexArray.add(index);
index = parsedContent.toLowerCase().indexOf(threeMonthArray[i].toLowerCase(), index + 1);
}
// System.out.println(threeMonthArray[i]+" : "+parsedContent.toLowerCase().indexOf(threeMonthArray[i].toLowerCase())+"------");
}
Collections.sort(indexArray);
System.out.println( indexArray);
它显示了这个输出:-
[2873, 2884, 3086, 3098, 4303, 4315, 6251, 6262, 8130, 8142, 15700, 15711]
我得到了正确的位置。我的问题是如何获得下一个必须是数字的单词。
Jun 2010 to Sep 2011 First Document
Jun 2009 to Aug 2011 Second Document
Nov 2011 – Sep 2012 Third Document
Nov 2012- Sep 2013 Forth Document
最佳答案
您可以使用正则表达式从上次找到的月份的位置开始查找下一个数字:
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(parsedContent);
if (m.find(index)) {
String year = m.group();
}
关于java - 在java中使用索引位置的单词后的下一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996352/