开发者

How to match this date format with a regex?

hi to all regex master out there, I know you have a work around with regards to my problem. hehe

开发者_开发技巧
02-May-2011

or

22-May-2011

or

2-May-2011

(dd-MMM-yyyy) with yyyy not accepting any other characters than digit


[0-9]{1,2}/[a-zA-Z]{3}/[0-9]{4}

That's assuming that the month is a 3-letter version: eg, Jan, Feb, Mar.

Updated version to match the changes to the question:

[0-9]{1,2}-[a-zA-Z]{3}-[0-9]{4}

As has been mentioned, this won't actually validate the date, it'll just validate that the string matches the format of: 1 or 2 numbers, a dash, 3 letters, a dash, 4 numbers.


^\d{1,2}/[a-zA-Z]+/\d{4}$

Is probably what you're looking for. Although the technically correct one is:

/^([12]\d|3[01])/(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|June?|July?|Aug(ust)?|Sep(t(ember)?)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)/\d{4}$/i

Sorry for not validating February and the number of days in a month, but there are some things just not worth doing in regular expressions ;)


Use SimpleDateFormat instead of using regexp. Read the tutorial at http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html for more info.


   /**
     * Created with IntelliJ IDEA.
     * User: S34N
     * Date: 2013/07/30
     * Time: 8:21 AM
     * To change this template use File | Settings | File Templates.
     */



    //Import the required classes/packages
    import javax.swing.*;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class dateInputScan {

        public static void main(String args[]) {

            dateInputScan run = new dateInputScan();
            run.dateInputScan();
        }

        public void dateInputScan() {

            //Instantiating variables
            String lvarStrDateOfTransaction = null;
            DateFormat formatter = null;
            Date lvarObjDateOfTransaction = null;

            //Use one of the following date formats.
            lvarStrDateOfTransaction = "29/07/2013";
            //lvarStrDateOfTransaction = "29-07-2013";
            //lvarStrDateOfTransaction = "20130729";
            //lvarStrDateOfTransaction = "2013-07-29";
            //lvarStrDateOfTransaction = "29/07/2013";

            //You can also add your own regex (Regular Expression)
            if (lvarStrDateOfTransaction.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})")) {
                formatter = new SimpleDateFormat("dd/MM/yyyy");
            } else if (lvarStrDateOfTransaction.matches("([0-9]{2})-([0-9]{2})-([0-9]{4})")) {
                formatter = new SimpleDateFormat("dd-MM-yyyy");
            } else if (lvarStrDateOfTransaction.matches("([0-9]{4})([0-9]{2})([0-9]{2})")) {
                formatter = new SimpleDateFormat("yyyyMMdd");
            } else if (lvarStrDateOfTransaction.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})")) {
                formatter = new SimpleDateFormat("yyyy-MM-dd");
            } else if (lvarStrDateOfTransaction.matches("([0-9]{4})/([0-9]{2})/([0-9]{2})")) {
                formatter = new SimpleDateFormat("yyyy/MM/dd");
            }

            try {
                lvarObjDateOfTransaction = formatter.parse(lvarStrDateOfTransaction);
                JOptionPane.showMessageDialog(null, "Date: " + lvarObjDateOfTransaction);

            } catch (Exception ex) {    //Catch the Exception in case the format is not found.
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    }


Okay, here are almost all possible regex variations of the Date pattern:

private final static Pattern DATE_PATTERN_1 = 

        Pattern.compile (
            "(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat) " + 
            "(?:Jan|Feb|Mar|Apr|May|June?|July?|Aug|Sept?|Oct|Nov|Dec) " + 
            "\\d\\d \\d\\d:\\d\\d:\\d\\d \\S+ \\d\\d\\d\\d", Pattern.CASE_INSENSITIVE);

private final static Pattern DATE_PATTERN_2 = 
        Pattern.compile (
            "\\d{4}.\\d{2}.\\d{2}", Pattern.CASE_INSENSITIVE);

private final static Pattern DATE_PATTERN_3 = 
        Pattern.compile (
            "\\d{2}.\\d{2}.\\d{4}", Pattern.CASE_INSENSITIVE);

private final static Pattern DATE_PATTERN_4 = 
        Pattern.compile (
            "([0-9]{4})([0-9]{2})([0-9]{2})", Pattern.CASE_INSENSITIVE);        

private final static Pattern DATE_PATTERN_5 = 
        Pattern.compile (
            "^([12]\\d|3[01]).(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|June?|July?|Aug(ust)?|Sep(t(ember)?)?|Oct(ober)?|Nov(ember)?|Dec(ember)?).\\d{4})$", Pattern.CASE_INSENSITIVE); 

Please note that "." character stand for any char.


Since it is date, which you are trying to find a match, using SimpleDateFormat instead of a regex based match check should be the appropriate solution. Hopefully, the below snippet should do the job.

public static boolean dateFormatValidate(String strDate,String dateFormat)
{
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
    String strParsed = null;
    Date parsed =null;

    try
    {
        parsed = formatter.parse(strDate);
        strParsed = formatter.format(parsed);
    }
    catch (ParseException e)
    {
        MYLogger.logger.info(e.getMessage());
    }

    return strDate.equals(strParsed);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜