java - 在java中使用索引位置的单词后的下一个数字

标签 java

我正在尝试解决这个问题:

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/

相关文章:

java - 如何在Java中的xml字符串中包含 "encoding="UTF-8""?

java - 逐行显示JTable

java - com.fasterxml.jackson.databind.exc.MismatchedInputException : Can not deserialize instance of object out of START_ARRAY token

java - 最终类上的 PowerMock UnfinishedStubbingException 错误

java - 填写预先格式化的文本文件以发送电子邮件 - MessageFormat 替代方案

java - java 的 href 链接

java - 如何保护客户端反作弊

java - Spring BeanUtils 无法实例化泛型类

java - while 循环出错。它不会阻止 Java

java - 如何在 Liferay 中提取嵌套的 JSON 元素