regex 正则表达式验证希伯来数字系统中的任何希伯来年

mcvgt66p  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(125)

我需要验证输入,以确保它是一个有效的希伯来语年数,它只能有某些有效的字母序列。
类似于(pseudo regex)([:thousands:]')?[:hundreds combinations:][:tens:]?("[:alef-tes digits:])?但有时它只有个位数,或十位数等,没有任何数百或数千...
不知道它的正式名称是什么,希伯来数字系统?希伯来数字命理学?希伯来数字系统?Gematria?概述如下所述:https://en.wikipedia.org/wiki/Hebrew_numerals在此测试:https://hebrewnumerals.github.io/示例:

  • 1 =
  • 15 ="
  • 16 ="
  • 42 ="
  • 133 ="
  • 499 ="
  • 501 ="
  • 651 ="
  • 765 ="
  • 872 = birth "
  • 1015 ="
  • 1724 ="
  • 2389 ="
  • 4129 ="
  • 4487 ="
  • 6298 ="
  • 7892 =
  • 9301 ="

一些无效年份的示例:

  • ז׳צת"ב (Taf 400 cannot be after Tzadik 90)
  • ר "
  • '
  • bad-should've been
  • "

(尝试搜索现有的库或日历源代码,给了regex101一堆尝试)

mzsu5hc0

mzsu5hc01#

我现在使用的PHP非正则表达式解决方法:
1.从反面开始;编写一个函数numberToHeb($num),将一个数字(例如4345)可靠地转换为一个正确表达的希伯来数字('")。目前正在使用内置的PHP日历转换函数,利用年数作为通用的转换数字到希伯来语的功能(限于1-9999范围)
1.现在创建一个函数hebNumber($str),以获取任何希望被验证的希伯来数(例如'"),如果它是任何希伯来数(在本例中为1-9999)的有效公式。在这个功能中;
1.将每个希伯来数字转换为数值('= 4000,=300,=40,=5),加总(4345),通过步骤#1中的numberToHeb($num)函数传递总数,检查结果字符串是否与hebNumber($str)函数的字符串匹配,如果匹配=通过验证,否则它不是一个正确格式的希伯来数字。

function numberToHeb(int $num){
    // limitation of using built in calendar converstion functions
    //  to convert number to hebrew representation: only handles 1-9999
    
    $x1 = mb_convert_encoding(
        jdtojewish(
            jewishtojd(1,1,$num), true,
            CAL_JEWISH_ADD_GERESHAYIM|CAL_JEWISH_ADD_ALAFIM_GERESH
        ),
        "UTF-8",
        "ISO-8859-8"
    );

    return mb_substr($x1, mb_strrpos($x1, ' ') + 1);

    // alternatively can port to PHP something like
    //    https://hebrewnumerals.github.io/ function Generate(...)
    // or https://github.com/MattMcManis/Aleph/blob/master/src/Aleph/Aleph/Converter.cs
}

function hebNumber(string $num){
    $lets = [
        // looks like StackOverflow code block reverses the text direction for RTL text, even the greater-than symbol looks like a less-than
        // but it's really "x" => N
        "א" => 1,
        "ב" => 2,
        "ג" => 3,
        "ד" => 4,
        "ה" => 5,
        "ו" => 6,
        "ז" => 7,
        "ח" => 8,
        "ט" => 9,
        "י" => 10,
        "כ" => 20,
        "ל" => 30,
        "מ" => 40,
        "נ" => 50,
        "ס" => 60,
        "ע" => 70,
        "פ" => 80,
        "צ" => 90,
        "ק" => 100,
        "ר" => 200,
        "ש" => 300,
        "ת" => 400,
    ];
    $parts = explode("'", $num); // if has thousands digit
    $main = end($parts);
    $total = 0;
    if (count($parts)>1) {
        // has thousands digit; add to total
        $yrpart = $parts[0];
        $main = $parts[1];
        $total = ($lets[$yrpart]??0) * 1000;
    }
    foreach (mb_str_split($main) as $char) {
        // simply add up each individual letter to total
        $total += $lets[$char] ?? 0;
    }

    // if valid hebrew input number, then input string should match same as generating the hebrew from total
    return numberToHeb($total) === $num ? $total : false;
}
>>> hebNumber("ד'שמ\"ה")
=> 4345
>>> hebNumber("ד'שה\"מ")
=> false

相关问题