0

I need regex for Bulgarian date format- d.M.yyyy 'г.' For date format- dd.MM.yyyy the below regex works:

/^\d\d?\.\d\d?\.\d\d\d?\d?$/

Need regex for Bulgarian date format- d.M.yyyy 'г.' No fruitful results found for my searches anywhere. plz help

AB te pach
  • 181
  • 1
  • 2
  • 4

1 Answers1

1

Just add the 'г. ' part.

/^\d\d?\.\d\d?\.\d\d\d?\d? ?'г\.'$/

Please note that that the regex actually doesn't validate dates. For example the '20.20.2020' is considered as valid date.

Edit for full validation: You can use something like this:

/^((31[.]0?[13578]|1[02][.](18|19|20)[0-9]{2})|((29|30)[.](0?1|0?[3-9]|1[1-2])[.](18|19|20)[0-9]{2})|((0?[1-9]|1[0-9]|2[0-8])[.](0?[1-9]|1[0-2])[.](18|19|20)[0-9]{2})|((29)[.](0?2)[.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))) 'г\.'$/

But as I said in the comment it's unreadable.

Also it's better to use "[0-9]" instead of "\d" because the last mean "all digits" not only 0123456789 but also these:

 ZERO:  0٠۰߀०০੦૦୦௦౦೦൦๐໐0
 ONE:   1١۱߁१১੧૧୧௧౧೧൧๑໑1
 TWO:   2٢۲߂२২੨૨୨௨౨೨൨๒໒2
 THREE: 3٣۳߃३৩੩૩୩௩౩೩൩๓໓3
 FOUR:  4٤۴߄४৪੪૪୪௪౪೪൪๔໔4
 FIVE:  5٥۵߅५৫੫૫୫௫౫೫൫๕໕5
 SIX:   6٦۶߆६৬੬૬୬௬౬೬൬๖໖6
 SEVEN: 7٧۷߇७৭੭૭୭௭౭೭൭๗໗7
 EIGHT: 8٨۸߈८৮੮૮୮௮౮೮൮๘໘8
 NINE:  9٩۹߉९৯੯૯୯௯౯೯൯๙໙9�� 

Source: Should I use \d or [0-9] to match digits in a Perl regex?

Community
  • 1
  • 1
Viktor Bahtev
  • 4,858
  • 2
  • 31
  • 40
  • thanks, is there any way to validate dates using regex?? – AB te pach Jun 16 '14 at 18:41
  • Yes but it's not a good idea. Your logic should be clear to the people that read your code. You should pay attention to different days in different months, leap years etc. The regex will be very ugly and completely unreadable. – Viktor Bahtev Jun 18 '14 at 14:31