1

Hi I want to be able to set the regular expression to allow for dates to be entered like this

01/01/1900 or 01/01/70, I have the following but not sure how to make it so that it takes 4 or 2 at the end.

^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$

The other one I would like to know is for URL

This one I have no idea how do I make it so that it matches correct URL's?

Thank you

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
user710502
  • 11,181
  • 29
  • 106
  • 161
  • 1
    What language are you using? Chances are there is a regular expression validation library that has the common cases like this. – Mark Thomas Jul 22 '11 at 18:41
  • For the year: `(\d\d|[12]\d\d\d)`. For URLs: this is tough, do you really want to check that in advance? Why not let the next higher protocol handle the validity of the URL. – Kerrek SB Jul 22 '11 at 18:42

5 Answers5

1

This should match two our four digit numbers:

\d{2}(\d{2})?

Your full regex would be something like this:

^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]\d{2}(\d{2})?$

URLs are hard to test. http://localhost is a valid URL and so it https://test.example.co.uk:443/index.ece?foo=bar. I would look for something in your language to test this for you or do a very simple test like this (you will have to delimit some special chars depending on the regex engine you use):

^https?://
gpojd
  • 22,558
  • 8
  • 42
  • 71
0

To modify your regex so that it takes either 2 or 4 digits at the end, you can try this:

^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}|[0-9]{2})$

For URLs, you can try (from here):

(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

or have a look at this S.O. question.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
0
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([0-9]{4}|[0-9]{2})$
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

Well, is ([0-9]{4}|[0-9]{2}) not good enough for you? Probably you could add some checking that first two digits in the four-digits group is 19 or 20 but it depends on your needs.

As for URL matching look here. There's many of them with tests.

Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
0

You can use another alternation in at the end to accept 2 or 4 (the same way you do the "or" options for the other date parts). Alternatively, you can require 2 digits in the last position, and then have 2 optional digits after that.

Unless you need to capture the individual parts (day, month, year), you should use non-capturing parentheses, like this (?:) (that's the .NET syntax).

Finally, you should consider the type of validation that you are trying to achieve with this. It is probably better to enforce the format, and not worry about bad forms like 91/73/9004 because even with what you have you can still get invalid dates, like 02/31/2011. Since you probably have to perform further validation, why not simplify the regex to something like ^(?:\d{1,2}[-/.]){2}\d{2}(?:\d{2})?$

As for URLs, stackoverflow is littered with duplicate questions about this.

Jay
  • 56,361
  • 10
  • 99
  • 123