如何使用模式匹配器为日期正则表达式?

时间:2016-02-24 23:34:24

标签: java regex date

如果我有例如1776年7月4日是美国的独立日 今天的日期是2016年2月25日我怎么可能得到" 7月4日1776"和" 02/25/2016"从句子中将它们更改为yyyy_date_month的格式?

public final static String regex = StringUtils.join(new String[]{
            String.format("(%s)", StringUtils.join(
                    //date
                    "((3[01]|2\\d|1\\d|0?\\d)" +
                    //ordinals
                    "(st|nd|rd|th)?(\\sof)?" +
                   // month,
                   "(\\s?(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)|jul(y)|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?))"+
                    //year
                    "?(\\d{4})?)"

            )),
            String.format("(%s)", StringUtils.join(//mm/dd/yyyy format
                    //month/
                    "^(0[1-9]|1[0-2])" +
                    // - | /
                    "(\\/|-)?" +
                    //date/
                   "(\\s0[1-9]|\\s1\\d|\\s2\\d|\\s3[01])" +
                   // - | /
                   "(\\/|-)?" +
                   //year
                   "(\\s\\d{4})"

            }, "|");
            public Dates() {

        Pattern[] patterns = new Pattern[regex.length()];
        for (int i = 0; i < regex.length(); i++) {
            patterns[i] = Pattern.compile(regex);
        }
        this.patterns = patterns;
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

public static void main (String[] args) throws java.lang.Exception
{
    Pattern p = Pattern.compile( "((\\w+)\\s*(\\d{1,2})\\s*(\\d{4}))|((\\d{1,2})\\/(\\d{1,2})\\/(\\d{4}))" );
    String input_1 = "July 4 1776 was bla bla";
    String input_2 = "02/25/2016";
    Matcher m = p.matcher( input_1 );
    String month = null;
    String day = null;
    String year = null;
    String formatted_date = null;
    if( m.find() )
    {
        System.out.println("Found groups :" + m.group() );
        String found = m.group();
        if( found.matches("((\\d{1,2})\\/(\\d{1,2})\\/(\\d{4}))") )
        {
            p = Pattern.compile("(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})");
            m = p.matcher(found);
            if( m.find() )
            {
               month = m.group(1);
               day = m.group(2);
               year = m.group(3);
               String formatteddate =  year + "_" + day + "_" + month;
               System.out.println(input_1.replaceAll("((\\d{1,2})\\/(\\d{1,2})\\/(\\d{4}))", formatteddate));
            }

        }
        else if( found.matches("((\\w+)\\s*(\\d{1,2})\\s*(\\d{4}))") )
        {
            p = Pattern.compile("(\\w+)\\s*(\\d{1,2})\\s*(\\d{4})");
            m = p.matcher(found);
            if( m.find() )
            {
               month = m.group(1);
               day = m.group(2);
               year = m.group(3);
               String formatteddate =  year + "_" + day + "_" + month;
               System.out.println(input_1.replaceAll("((\\w+)\\s*(\\d{1,2})\\s*(\\d{4}))", formatteddate));
            }


        }


    }
    else
    {
        System.out.println("No groups found");
    }
}

public static boolean isInteger(String s) 
{
   try 
   { 
     Integer.parseInt(s); 
   } catch(NumberFormatException e) 
   { 
     return false; 
   } catch(NullPointerException e) 
   {
     return false;
   }

   return true;
}

请参阅演示here