[英]How to rewrite this PHP function to ruby?
我是紅寶石和正則表達式的新手,並且我用PHP編寫了此函數,因此需要將其重寫為Ruby。
function date_number_id($id_number) {
if (preg_match('~^([0-9]{2})([0-9]{2})([0-9]{2})/([0-9]{3,4})$~', $id_number, $match)) {
return (strlen($match[4]) < 4 || $match[1] >= 54 ? "19" : "20") . "$match[1]-" . sprintf("%02d", $match[2] % 50) . "-$match[3]";
}
else
{
return false;
}
}
你能幫我嗎?
非常感謝!
編輯:這是從生日那天的識別號打印的功能。 輸入例如: 890807/5089
除非我錯過任何事情...
def date_number_id(id_number)
match = /^([0-9]{2})([0-9]{2})([0-9]{2})\/([0-9]{3,4})$/.match(id_number)
if match
return "#{match[4].to_i < 4 || match[1].to_i >= 54 ? '19' : '20'}#{match[1]}-#{sprintf("%02d", match[2].to_i % 50)}-#{match[3]}"
else
return false
end
end
我認為到PHP的映射是顯而易見的,但是如果您不了解某些原因,請詢問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.