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

java中使用索引位置的单词后的下一位数字,java,Java,我试图解决这个问题: 我的逻辑是,首先我们得到字符串中月份的所有位置,然后我提取下一个单词,它是4位数或2位数的年份,然后用它计算差值 为了获得这个职位,我使用以下代码:- String[] threeMonthArray=new String[]{" Jan "," Feb "," Mar "," Apr "," May "," June "," July "," Aug "," Sep "," Oct "," Nov "," Dec "}; String[]

我试图解决这个问题:

我的逻辑是,首先我们得到字符串中月份的所有位置,然后我提取下一个单词,它是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);
我得到了正确的位置。我的问题是如何得到下一个必须是数字的单词

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();
}

parsedContent.toLowerCase()
中的
parsedContent
是什么?哇。工作谢谢我会张贴原始问题的解决方案,如果我得到。
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(parsedContent);
if (m.find(index)) {
    String year = m.group();
}