Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何搜索以同一数组中的其他单词开头和结尾的单词?_Javascript_Regex_Arrays_Perl_String - Fatal编程技术网

Javascript 如何搜索以同一数组中的其他单词开头和结尾的单词?

Javascript 如何搜索以同一数组中的其他单词开头和结尾的单词?,javascript,regex,arrays,perl,string,Javascript,Regex,Arrays,Perl,String,我在一个数组中有一长串单词。有的短,有的长。我想过滤掉那些以数组中的一个单词开头的单词(这个“前缀”单词的长度可以设置为,比如说,3个字符),同时以数组中的一个单词结尾的单词 假设第一个词是“车棚”。现在,如果数组中也存在“car”和“port”,我会得到一个匹配项。但是如果这个词是“嘉士伯”,我就找不到匹配的词(因为“lsberg”可能不是数组中现有的词) 结果最好是“前缀词、后缀词、整个词” 我会考虑使用任何能使我做到这一点的语言,虽然我自己主要是一个JavaScript的人。 嗯,在Jav

我在一个数组中有一长串单词。有的短,有的长。我想过滤掉那些以数组中的一个单词开头的单词(这个“前缀”单词的长度可以设置为,比如说,3个字符),同时以数组中的一个单词结尾的单词

假设第一个词是“车棚”。现在,如果数组中也存在“car”和“port”,我会得到一个匹配项。但是如果这个词是“嘉士伯”,我就找不到匹配的词(因为“lsberg”可能不是数组中现有的词)

结果最好是“前缀词、后缀词、整个词”


<>我会考虑使用任何能使我做到这一点的语言,虽然我自己主要是一个JavaScript的人。

嗯,在JavaScript中天真的实现方式是这样的:

function triples(words) { 
    var result = new Array();
    for(var i=0; i<words.length; i++) {
        for(var j=0; j<words.length; j++) {
            var k = words.indexOf(words[i] + words[j]);
            if(k != -1) {
                result.push([words[i], words[j], words[k]]);
            }
        }
    } 
    return result;
}
#!/usr/bin/perl

use strict;
use warnings;

my @candidates=qw( carport Carsburg butterfly 
                buttercup Christmas wishlist carpface flyface buttface);
my @arr=<DATA>;
chomp @arr;

for my $i (3..6) {
    foreach my $j (@candidates) {
        my ($fp,$lp)=($1,$2) if ($j=~/(^.{$i})(.*$)/);
        if($fp && $lp) {
            my @hit1=grep(/^$fp/,@arr);
            my @hit2=grep(/$lp$/,@arr);
            print "candidate: $j\n start= @hit1 end= @hit2\n=====\n" 
                if (scalar @hit1 && scalar @hit2);
        }
    }
}

__DATA__
car
port
wish
list
Christ
mas
butter
cup
fly
face
butt
函数三元组(字){
var result=新数组();
对于(var i=0;i类似这样的情况:

function triples(words) { 
    var result = new Array();
    for(var i=0; i<words.length; i++) {
        for(var j=0; j<words.length; j++) {
            var k = words.indexOf(words[i] + words[j]);
            if(k != -1) {
                result.push([words[i], words[j], words[k]]);
            }
        }
    } 
    return result;
}
#!/usr/bin/perl

use strict;
use warnings;

my @candidates=qw( carport Carsburg butterfly 
                buttercup Christmas wishlist carpface flyface buttface);
my @arr=<DATA>;
chomp @arr;

for my $i (3..6) {
    foreach my $j (@candidates) {
        my ($fp,$lp)=($1,$2) if ($j=~/(^.{$i})(.*$)/);
        if($fp && $lp) {
            my @hit1=grep(/^$fp/,@arr);
            my @hit2=grep(/$lp$/,@arr);
            print "candidate: $j\n start= @hit1 end= @hit2\n=====\n" 
                if (scalar @hit1 && scalar @hit2);
        }
    }
}

__DATA__
car
port
wish
list
Christ
mas
butter
cup
fly
face
butt
我不知道a是否能帮上忙,看

Perl有几个模块来构建它们:

另一个听起来像是开始的地方是模块:


下面是一个Perl解决方案,它是
O(n+2m)

印刷品:

$VAR1 = [
      [
        'car',
        'carport',
        'port'
      ],
      [
        'car',
        'cartographer',
        'grapher'
      ],
      [
        'air',
        'airport',
        'port'
      ]
    ];

我会这样做:

<?php

    $words = array('experts', 'exchange', 'expert', 'sexchange');

    // build trie
    $t = array();
    foreach ($words as $word)
    {
        $n = &$t;
        for ($i = 0; $i < strlen($word); ++$i)
        {
            $c = $word[$i];

            if (!isset($n[$c])) $n[$c] = array();

            $n = &$n[$c];
        }

        $n['.'] = true;
    }

    $word = 'expertsexchange';

    $n = $t;
    for ($i = 0; $i < strlen($word); ++$i)
    {
        $c = $word[$i];

        if (isset($n['.']))
        {
            $o = $t;
            for ($j = $i; $j < strlen($word); ++$j)
            {
                $d = $word[$j];
                if (!isset($o[$d])) break;
                $o = $o[$d];                    
            }

            # found match
            if ($j == strlen($word) && isset($o['.']))
            {
                echo substr($word, 0, $i).",".substr($word,$i).",".$word."\n";
            }
        }

        if (isset($n[$c]))
        {
            $n = $n[$c];
        }
        else
            break;
    }
?>

Results:

expert,sexchange,expertsexchange
experts,exchange,expertsexchange

结果:
专家
专家、交流、专家改变

我是当场写的,所以它可能不完全正确。但想法是建立一个前缀树并遍历它。每次找到前缀时(以“.”表示),从树的顶部再次继续,查看是否可以从该点找到后缀。这假设前缀和后缀之间不需要任何内容。

您自己尝试过吗?您能发布到目前为止的内容吗?谢谢。您说的是“任何语言”-这是针对web应用程序的吗?如果是,您使用的服务器技术是什么?我们是否可以访问PHP/PERL/ASP?如果这只是一个页面重新加载,您可能会在服务器端获得更好的性能。如果您可以提供更多信息,我将尽力为您提供解决方案:)这将是一次“运行一次”生成一个新文件的方法。我昨晚才尝试了一些regexp,但我想问问你们是否有任何优雅的解决方案,不管是哪种语言(我知道有些语言比其他语言更适合不同类型的任务)。对到目前为止的(快速!)响应感到惊讶,非常感谢!拥有“carport”(和其他语言)该列表中的“组合”字表示“end=carport port”“虽然如此,但我想你已经接近我想要的了。可能是筛选出具有多个开头和结尾的匹配项?我正在考虑对大量文本使用此筛选器,甚至可能是某种字典,所以我认为每个开头词无论如何都必须出现?我不确定我是否理解。你是说你添加了“carport”吗?”对于
\uuu DATA
下的列表?如果您希望基于单个列表而不是两个列表(我编写的方式)进行这种类型的筛选,则逻辑略有不同。很抱歉,回复太晚。是的,我的目标是一个列表。indata可能是某种词汇表,用于查找由列表中的其他单词组成的单词。
<?php

    $words = array('experts', 'exchange', 'expert', 'sexchange');

    // build trie
    $t = array();
    foreach ($words as $word)
    {
        $n = &$t;
        for ($i = 0; $i < strlen($word); ++$i)
        {
            $c = $word[$i];

            if (!isset($n[$c])) $n[$c] = array();

            $n = &$n[$c];
        }

        $n['.'] = true;
    }

    $word = 'expertsexchange';

    $n = $t;
    for ($i = 0; $i < strlen($word); ++$i)
    {
        $c = $word[$i];

        if (isset($n['.']))
        {
            $o = $t;
            for ($j = $i; $j < strlen($word); ++$j)
            {
                $d = $word[$j];
                if (!isset($o[$d])) break;
                $o = $o[$d];                    
            }

            # found match
            if ($j == strlen($word) && isset($o['.']))
            {
                echo substr($word, 0, $i).",".substr($word,$i).",".$word."\n";
            }
        }

        if (isset($n[$c]))
        {
            $n = $n[$c];
        }
        else
            break;
    }
?>

Results:

expert,sexchange,expertsexchange
experts,exchange,expertsexchange