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



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 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:


    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:


    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.

    点赞 评论 复制链接分享
  • doubingjiu3199 doubingjiu3199 2012-11-30 19:00

    Day of month (up to 39); month of year (up to 19); year 00 to 99; century, and 4 digits.

    That's a pretty generic regex. There are lots of other ways it could be written; some of them might be clearer, and a few might be faster. You could make the ranges more exact using notations such as:


    which only accepts values to 31 for the day of month. However, you'd still need to reject 31st February, which is not something you want to try encoding in a regex (it could be done, no doubt; you just don't want to try).

    点赞 评论 复制链接分享
  • dongpiao9078 dongpiao9078 2012-11-30 19:05
    • ^ - beginning of the string
    • (0[1-9]|[12][0-9]|3[01]) 01-09, 10-29, 30 or 31 (days)
    • (0[1-9]|1[012]) 01-09, 10, 11, or 12 (month)
    • (\d{2}) any two digits (year)
    • - a dash
    • (0|1|2) 0, 1, or 2 (the century digit)
    • (\d{4}) any four digits

    Note that this will not handle some cases of invalid dates, such as February 31st, etc. This is essentially impossible to do with just a regular expression.

    点赞 评论 复制链接分享
  • doutusheng5879 doutusheng5879 2012-11-30 19:09

    It's not a regular expression, and it's not the prettiest code, but you can get all the bits out of the ID with this code:

    $input = '080189-12602';
    $id_arr = explode('-', $input);
    list($day, $month, $year) = str_split($id_arr[0], 2);
    $arr = str_split($id_arr[1]);
    $century = 18 + $arr[0];
    $id = implode('', array_slice($arr, -4));
    printf("%s -> %s%s/%s/%s-%s", $input, $century, $year, $month, $day, $id);
    //output: 080189-12602 -> 1989/01/08-2602

    Given the wackiness of dates it's probably not advisable to try validating them with a regular expression. eg: try to write one that will tell you if February 29 is a valid date. It's best to chop up your input and run it through some date library where all of the checks are already implemented.

    点赞 评论 复制链接分享