繁体   English   中英

将人类可读的数字范围转换为正则表达式

[英]Convert human readable number range to Regex

我有一个使用数据表显示的表,在每一列的上方都有一个空的文本表单字段,用户可以根据其输入内容进行过滤。 这在所有文本字段上都可以正常工作,在整数字段上也可以。 我正在对某些术语进行某种转换,例如,如果用户键入NULL或NOT NULL,则将其转换为regex ^ $或。

我知道正则表达式旨在搜索文本字符串,但这就是数据表所使用的,因此这就是我这样做的原因。 我想要的是用户能够输入“ x to y”之类的值并将其转换为正则表达式。 我找不到执行此操作的函数,如果有人知道一个函数,请告诉我。

假设尚不存在函数,则假设仅搜索正整数,并说最多7位数字。 因此可以搜索0-9,999,999。 同样,触发此方法的唯一方法是使用关键字to,使其带有空格“至”。

所以这样的事情开始:

function convertNumRangeRegex(s){

if(s.indexOf(" to ") != -1){
var range = s.split(" to ");
lowRange = Number(range[0]);
highRange = Number(range[1]);   

if(lowRange >= 0 && lowRange < 10 && highRange < 10){
        s = "^[" + lowRange + "-" + highRange + "]$";
}};


return s; 
};

这适用于数字0-9,但对此进行扩展似乎很难看。 我支持任何想法。 谢谢。

使用正则表达式验证数字是否在数字范围内是一个棘手的问题。 这些正则表达式将匹配给定范围内的数字:

\b[0-9]{1,7}\b        #   0-9999999
\b[1-9][0-9]{2,6}\b   # 100-9999999
\b([4-9][0-9]{4}|[1-9][0-9]{5,6})\b   # 40000-9999999

当您有复杂的范围时,它开始失控

\\b(?:5(?:4(?:3(?:2[1-9]|[3-9][0-9])|[4-9][0-9]{2})|[5-9][0-9]{3})|[6-9][0-9]{4}|[1-9][0-9]{5}|[1-8][0-9]{6}|9(?:[0-7][0-9]{5}|8(?:[0-6][0-9]{4}|7(?:[0-5][0-9]{3}|6(?:[0-4][0-9]{2}|5(?:[0-3][0-9]|4[0-3]))))))\\b # 54321-9876543

在此处输入图片说明

向前

3年后,我重新发现了这个问题,并花了一些时间解决这个难题。 对于您为什么要使用正则表达式,我尚不完全清楚,但是我敢肯定,这与通过不将所有可能的结果强制提供给客户端进行评估来提高数据库的返回性能有关。

就是说,我确定您有您的理由。

说明

这组函数将构造一个执行以下操作的正则表达式:

  • 验证给定数字在给定范围内的正整数之间
  • 拒绝超出该范围的数字
  • 要求整个字符串为数字
  • 适用于任何大小的数字
  • 允许整个范围包括上限和下限

总体概述

函数funRegexRange完成所有繁重的工作。 通过构建将匹配从0UpperRange所有数字的字符串

然后,函数funBuildRegexForRange构造具有负先行和正先行的实际正则表达式。

然后,生成的正则表达式将验证您的数字是否介于0UpperRange之间(包括UpperRange ,而不是介于0LowerRange之间(不包括LowerRange

该函数将允许数字或字符串值,但不验证输入是否为整数。 提供不等于整数的值将产生不可预测的结果。

例子

要获取介于400到500之间的正则表达式:

re = funBuildRegexForRange( 400, 500, true ) 

通过将最后一个参数设置为true,将显示正在构造的各个部分以及完整的正则表达式。

[0-3][0-9]{2}, [0-9]{1,2}
[0-4][0-9]{2}, 500, [0-9]{1,2}
Full Regex = /^(?!(?:[0-3][0-9]{2}|[0-9]{1,2})$)(?=(?:[0-4][0-9]{2}|500|[0-9]{1,2})$)/

生成的正则表达式看起来像

正则表达式可视化

要求范围在400-999999999999 [十二个数字]之间,则会返回此怪物:

Full Regex = /^(?!(?:[0-3][0-9]{2}|[0-9]{1,2})$)(?=(?:[0-8][0-9]{11}|9[0-8][0-9]{10}|99[0-8][0-9]{9}|999[0-8][0-9]{8}|9999[0-8][0-9]{7}|99999[0-8][0-9]{6}|999999[0-8][0-9]{5}|9999999[0-8][0-9]{4}|99999999[0-8][0-9]{3}|999999999[0-8][0-9]{2}|9999999999[0-8][0-9]|99999999999[0-8]|999999999999|[0-9]{1,11})$)/

JavaScript代码

实时示例: https//repl.it/CLd4/4

完整代码:

function funRegexRange (UpperRange, Inclusive, Debug) {
    // this function will build a basic regex that will match a range of integers from 0 to UpperRange

    UpperRange += "" // convert the value to a string
    var ArrUpperRange = UpperRange.split("")
    var intLength = ArrUpperRange.length
    var LastNumber = ArrUpperRange[intLength]
    var AllSubParts = []
    var SubPortion = ""

    for (i = 0; i < intLength; i++) {
        Position = intLength - (i +1)
        if ( Position >= 2 ) { 
            Trailing = "[0-9]{" + Position + "}";
        } else if ( Position == 1 ) {
            Trailing = "[0-9]";
        } else {
            Trailing = "";
        }

        if ( ArrUpperRange[i] >= 2 ) {
            ThisRange = "[0-" + (ArrUpperRange[i] - 1) + "]"
        } else if ( ArrUpperRange[i] == 1 ) {
            ThisRange = "0"
        } else {
            ThisRange = ""
        }

        if ( Debug ) {  
        // console.log( "Pos='" + Position + "' i='" + i + "' char='" + ArrUpperRange[i] + "' ThisRange='" + ThisRange + "' Trailing='" + Trailing + "'")
        }

        if ( ThisRange === "" && Trailing !== "" ) {
            // no need to return the this as this will be matched by the future SubPortions
        } else {
            if ( Position === 0 && ThisRange ==="" && Trailing === "") {
            } else {
                AllSubParts.push( SubPortion + ThisRange + Trailing);
        }
        }
    SubPortion += ArrUpperRange[i]
    }

    // insert the last number if this it should be included in the range
    if ( Inclusive ) {
        AllSubParts.push(UpperRange)
    }

    // all all numbers that have less digits than the range
    if ( intLength - 1 >= 2 ) { 
        Trailing = "[0-9]{1," + ( intLength - 1 ) + "}";
    } else if ( intLength - 1 >= 1 ) {
        Trailing = "[0-9]";
    } else {
        Trailing = "";
    }

    // insert trailing into the output stream
    if ( Trailing ){
        AllSubParts.push( Trailing );
    }   

    if ( Debug ) {
        console.log(AllSubParts.join(", "));
    } 
    return AllSubParts.join("|");
} // end function funRegexRange


function funBuildRegexForRange ( Start, End, Debug ){
    var Regex = new RegExp("^(?!(?:" + funRegexRange (LowerRange, false, Debug) + ")$)(?=(?:" + funRegexRange (UpperRange, true, Debug) + ")$)" ,"" )   
    if ( Debug ) {
        console.log("Full Regex = " + Regex + "")
    }
    return Regex
}

var Debug = false;
var Inclusive = true;
var LowerRange = "400";
var UpperRange = "500";

// var re = funBuildRegexForRange( LowerRange, UpperRange, true ) 

if ( Debug ){
    for (Range = 0; Range < 13; Range++) {
        console.log ("funRegexRange ('" + Range + "', " + Inclusive +") =");
        funRegexRange (Range, Inclusive, Debug);
        console.log ("");
    }
}

var Regex = funBuildRegexForRange( LowerRange, UpperRange, Debug ) 

for (i = 1000; i < 1020; i++) {
    TestNumber = i + ""
    if ( TestNumber.match(Regex)) {
        console.log(TestNumber + " TestNumber='" + TestNumber + "' matches");
    } else {
//      console.log(TestNumber + " does not match '" + Regex + "'")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM