Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expressions to check length with multiple options

Tags:

date

regex

I need to validate the date format, that can be either 11/11/11 or 11/22/2013, i.e. the year block can be in YY or YYYY and the complete format will either MM/DD/YY or MM/DD/YYYY

I've this code

^(\d{1,2})\/(\d{1,2})\/(\d{4})$

and I've tried

^(\d{1,2})\/(\d{1,2})\/(\d{2}{4})$ // doesn't works, does nothing

and

^(\d{1,2})\/(\d{1,2})\/(\d{2|4})$ // and it returns null every time

PS: I'm applying it with Javascript/jQuery

like image 802
zzlalani Avatar asked Dec 20 '22 01:12

zzlalani


2 Answers

^(\d{1,2})\/(\d{1,2})\/(\d{2}|\d{4})$

Both \d{2}{4} and \d{2|4} are not correct regex expression. You have to do two digits and for digits separately and combine then using or: (\d{2}|\d{4})

like image 110
MarcinJuraszek Avatar answered Mar 08 '23 23:03

MarcinJuraszek


You could use:

^\d\d?/\d\d?/\d\d(?:\d\d)?$

explanation:

The regular expression:

(?-imsx:^\d\d?/\d\d?/\d\d(?:\d\d)?$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  \d?                      digits (0-9) (optional (matching the most
                           amount possible))
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  \d?                      digits (0-9) (optional (matching the most
                           amount possible))
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
like image 22
Toto Avatar answered Mar 08 '23 23:03

Toto