简体   繁体   中英

how to use pattern matcher for date regex?

If i have for example july 4 1776 was the independence day of usa and today's date is 02/25/2016 how could i possibly get "july 4 1776" and "02/25/2016" from the sentence and change them to a format with 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;
    }

You can try some thing like this:

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

See demo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM