doufei5537 2012-11-30 18:54
浏览 18
已采纳

拉脱维亚公民个人密码的正则表达式[已结束]

I have problem with writing regular expression ( php / js ) for Latvian citizen personal codes. Every citizen in Latvia has own personal code. The format is 11 digits and after the sixth digit there is dash. So first 6 digits are date of birth.

ddmmyy ( at example: 130589 ) then goes dash ( - ) then goes seventh digit, that can be 0, 1 or 2 which means century ( 0 for 19. century and 2 for 21. century ), and then goes digits from 0000 to 9999

Examples:

080189-12602

08   - day
01   - month
89   - year
-    - dash
1    - 20th century
2602 - random 4 digit combination.

So question is can someone help me with writing regular expression ?

  • 写回答

4条回答 默认 最新

  • doushun1870 2012-11-30 19:01
    关注

    I recommend that you do not validate the date with the regex. Instead check only for the correct format:

    /^(\d{6})-[012]\d{4}$/
    

    That matches 6 arbitrary digits, a dash, then a digit from 0 to 2, then 4 more arbitrary digits. Afterwards you will find the date part in capturing group number 1, which you can check for a valid date. Alternatively you could extract days, months, years separately:

    /^(\d{2})(\d{2})(\d{2})-[012]\d{4}$/
    

    Now the days are in capture 1, the months in capture 2, the years in capture 3. The validation of the date ranges could in theory be done with the regex but it really not recommended (the regex becomes really bloated and unmaintainable). That is why you should just check that there are digits, and then you can use your programming language (PHP or JS) to check that the given numbers form a valid date.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题