具有不同输入的Javascript正则表达式

具有不同输入的Javascript正则表达式,javascript,regex,Javascript,Regex,我想从一长段文字中过滤掉以下信息。我抄的 并粘贴到一个文本字段中,然后希望将处理结果放入一个表中。与 名字 地址 地位 示例片段:(名称和地址等的随机化) 我想从中得到的是: DUMMY FOO Stationstreet 2 8000 New York Sober FO DUMMY FOO Butterstreet 6 8740 Melbourne Sober JOHN FOOO Waterstreet 1 9990 Rome Sober FO SMOOTH M.FOO Queen Eli

我想从一长段文字中过滤掉以下信息。我抄的 并粘贴到一个文本字段中,然后希望将处理结果放入一个表中。与

  • 名字
  • 地址
  • 地位
示例片段:(名称和地址等的随机化)

我想从中得到的是:

DUMMY FOO Stationstreet 2 8000 New York Sober
FO DUMMY FOO Butterstreet 6 8740 Melbourne Sober
JOHN FOOO Waterstreet 1 9990 Rome Sober
FO SMOOTH M.FOO Queen Elisabethstreet 19 9990 Paris Not sober
我目前的策略是使用以下方法:

  • 在行首至少用两个大写字母过滤所有行。和一个4位数的邮政编码
  • 然后丢弃所有其他行,因为我只需要带有姓名和地址的行
  • 然后我去掉了那条线需要的所有信息
  • 删除名称/地址/状态
我使用以下代码:

  //Regular expressions

    //Filter all lines which start with at least two UPPERCASE words following a space
    pattern = /^(([A-Z'.* ]{2,} ){2,}[A-Z]{1,})(?=.*BSN)/;
    postcode = /\d{4}/;
    searchSober= /(N - Sober)+/;
    searchNotSober= /(NN - Not sober)+/;

    adres = inputText.split('\n');


    for (var i = 0; i < adres.length; i++) {

        // If in one line And a postcode and which starts with at least
        // two UPPERCASE words following a space
        temp = adres[i]

        if (  pattern.test(temp) && postcode.test(temp)) {

            //Remove BSN in order to be able to use digits to sort out the postal code
            temp = temp.replace( /BSN.*/g, "");

            // Example: DUMMY FOO V Stationstreet 2 8000 New York F N - Sober

            //Selection of the name, always take first part of the array
            // DUMMY FOO
            var name = temp.match(/^([-A-Z'*.]{2,} ){1,}[-A-Z.]{2,}/)[0];

            //remove the name from the string
            temp = temp.replace(/^([-A-Z'*.]{2,} ){1,}[-A-Z.]{2,}/, "");
            // V Stationstreet 2 8000 New York F N - Sober

            //filter out gender
            //Using jquery trim for whitespace trimming
            // V
            var gender = $.trim(temp.match(/^( [A-Z'*.]{1} )/)[0]);

            //remove gender
            temp = temp.replace(/^( [A-Z'*.]{1} )/, "");

            // Stationstreet 2 8000 New York F N - Sober
            //looking for status

            var status = "unknown";
            if ( searchNotsober.test(temp) ) {
                status = "Not soberr";
            }
            else if ( searchSober.test(temp) ) {
                status = "Sober";


            }
            else {
                status = "unknown";
            }

            //Selection of the address /^.*[0-9]{4}.[\w-]{2,40}/
            //Stationstreet 2 8000 New York
            var address = $.trim(temp.match(/^.*[0-9]{4}.[\w-]{2,40}/gm));

            //assemble into person object.
            var person={name: name + "", address: address + "", gender: gender +"", status:status + "", location:[] , marker:[]};
            result.push(person);
        }
    }
//正则表达式
//筛选以空格后至少两个大写单词开头的所有行
模式=/^(([A-Z.*]{2,}){2,}[A-Z]{1,})(?=.*BSN)/;
邮政编码=/\d{4}/;
searchSober=/(N-Sober)+/;
searchNotSober=/(NN-未清醒)+/;
adres=inputText.split('\n');
对于(变量i=0;i
我现在的问题是:

  • 有时名字不是用大写字母写的
  • 有时邮政编码没有添加,所以我的代码就停止工作了
  • 有时他们会在名字前面加一个*
一个更广泛的问题是,您可以采取什么策略来解决这些混乱的输入问题? 我应该为我在这些片段中看到的每一个错误辩护吗?我觉得 我真的不知道每次运行这段代码都能从中得到什么
它使用不同的输入。

以下是处理它的一般方法:

  • 查找最可能匹配的所有行。在“清醒”或其他任何情况下比赛都不可能错过,即使它会给你带来误报

  • 过滤掉误报,这一点你必须更新和调整你去。确保你只过滤掉那些根本不相关的东西

  • 严格过滤输入,记录/报告不匹配的内容以进行手动处理,现在匹配的内容符合已知的严格模式

  • 规范化和提取数据现在应该容易得多,因为在这个阶段您可能的输入有限


  • 为了编写一个程序来做某事,您需要能够准确地描述它应该做什么。如果你不知道如何处理不一致的输入,你就不能编写一个程序来处理它。我可以添加一些检查最常见错误的东西?问题是,作为一个人类,很明显可以看到名字是什么,等等,或者什么是错的。我想知道如何使我的程序更具知识性这就是为什么大多数程序不允许自由形式的输入,它们有单独的地址、邮政编码等字段。当有已知的输入列表时,它们使用菜单。如果不允许灵活的输入,就不必编写复杂的解析器。处理自然语言很难。我同意,但我无法控制输入,所以我必须尝试使用我想的正则表达式来解决它。虽然你可以编写正则表达式,但对于含糊不清的行,你会问用户它是否是地址,处理时是什么。感谢指导。我已经做的是根据BSN单词更改我的正则表达式,它总是在那里。我将不得不进行一个手动步骤,在该步骤中,我将标记不确定的内容。基于@maraca评论
      //Regular expressions
    
        //Filter all lines which start with at least two UPPERCASE words following a space
        pattern = /^(([A-Z'.* ]{2,} ){2,}[A-Z]{1,})(?=.*BSN)/;
        postcode = /\d{4}/;
        searchSober= /(N - Sober)+/;
        searchNotSober= /(NN - Not sober)+/;
    
        adres = inputText.split('\n');
    
    
        for (var i = 0; i < adres.length; i++) {
    
            // If in one line And a postcode and which starts with at least
            // two UPPERCASE words following a space
            temp = adres[i]
    
            if (  pattern.test(temp) && postcode.test(temp)) {
    
                //Remove BSN in order to be able to use digits to sort out the postal code
                temp = temp.replace( /BSN.*/g, "");
    
                // Example: DUMMY FOO V Stationstreet 2 8000 New York F N - Sober
    
                //Selection of the name, always take first part of the array
                // DUMMY FOO
                var name = temp.match(/^([-A-Z'*.]{2,} ){1,}[-A-Z.]{2,}/)[0];
    
                //remove the name from the string
                temp = temp.replace(/^([-A-Z'*.]{2,} ){1,}[-A-Z.]{2,}/, "");
                // V Stationstreet 2 8000 New York F N - Sober
    
                //filter out gender
                //Using jquery trim for whitespace trimming
                // V
                var gender = $.trim(temp.match(/^( [A-Z'*.]{1} )/)[0]);
    
                //remove gender
                temp = temp.replace(/^( [A-Z'*.]{1} )/, "");
    
                // Stationstreet 2 8000 New York F N - Sober
                //looking for status
    
                var status = "unknown";
                if ( searchNotsober.test(temp) ) {
                    status = "Not soberr";
                }
                else if ( searchSober.test(temp) ) {
                    status = "Sober";
    
    
                }
                else {
                    status = "unknown";
                }
    
                //Selection of the address /^.*[0-9]{4}.[\w-]{2,40}/
                //Stationstreet 2 8000 New York
                var address = $.trim(temp.match(/^.*[0-9]{4}.[\w-]{2,40}/gm));
    
                //assemble into person object.
                var person={name: name + "", address: address + "", gender: gender +"", status:status + "", location:[] , marker:[]};
                result.push(person);
            }
        }