Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to check date

Tags:

python

regex

Hi i have written regex to check where ther string have the char like - or . or / or : or AM or PM or space .The follworig regex work for that but i want to make case fail if the string contain the char other than AMP . import re

Datere = re.compile("[-./\:?AMP ]+")

FD = { 'Date' : lambda date : bool(re.search(Datere,date)),}

def Validate(date):

    for k,v in date.iteritems():
        print k,v
        print FD.get(k)(v)

Output:

Validate({'Date':'12/12/2010'})
Date 12/12/2010
True
Validate({'Date':'12/12/2010 12:30 AM'})
Date 12/12/2010
True

Validate({'Date':'12/12/2010 ZZ'})
Date 12/12/2010
True  (Expecting False)

Edited: Validate({'Date':'12122010'}) Date 12122010 False (Expecting False)

How could i find the string have other than the char APM any suggestion.Thanks a lot.

like image 552
Shashi Avatar asked Oct 08 '22 18:10

Shashi


1 Answers

Give this a try:

^[-./\:?AMP \d]*$

The changes to your regex are

  • It's anchored with ^ and $ which means that the whole line should match and not partially
  • the \d is added to the character class to allow digits

Now the regex basically reads as list of symbols that are allowed on 1 lines

If you want the empty string not to match then change the * to a +

like image 191
buckley Avatar answered Oct 10 '22 10:10

buckley