2

我正在为一些游戏创建天梯系统,但遇到了关于氏族基础系统的问题。你看,每个加入的玩家都会被解析并放入玩家表中。像这样:

chelsea | gordon 
chelsea | jim
chelsea | brad

或者...

CLANTAG|> jenna
CLANTAG|> jackson
CLANTAG|> irene 

所以,我想要的是:我想抓住 CLANTAG,它在同一个地方,并且在那个团队中的每个球员的名字都相同。但是,分隔符可以是从空白到无的任何内容(clan player1、clan player2 或 clanplayer1、clanplayer2)。

关于如何做到这一点的任何想法?

提前致谢。

4

4 回答 4

4

这是一个镜头:

use strict;
use warnings;

my($strip) = shift || 0;

print FindTeamName("TEAMJimBob", "TEAMJoeBob", "TEAMBillyBob"), "\n";
print FindTeamName("TEAM|JimBob", "TEAM|JoeBob", "TEAM|BillyBob"), "\n";
print FindTeamName("TEAM | JimBob", "TEAM | JoeBob", "TEAM | BillyBob"), "\n";
print FindTeamName("TEAMJimBob", "TEAM|JoeBob", "TEAM - BillyBob"), "\n";

sub FindTeamName
{
    my(@players) = @_;

    my($team) = shift;
    foreach my $player (@players) {
        $team = FindCommonString($team, $player);
    }

    $team =~ s{\W+$}{} if $strip;

    $team;
}

sub FindCommonString
{
    my($str1, $str2) = @_;

    my(@arr1) = split(//, $str1);
    my(@arr2) = split(//, $str2);

    my($common) = "";

    while (@arr1 && @arr2) {
        my($letter1) = shift(@arr1);
        my($letter2) = shift(@arr2);

        if ($letter1 eq $letter2) {
            $common .= $letter1;
        }
        else {
            last;
        }
    }

    $common;
}

这给出了以下内容:

C:\temp>perl test.pl
TEAM
TEAM|
TEAM |
TEAM

C:\temp>perl test.pl 1
TEAM
TEAM
TEAM
TEAM

C:\temp>
于 2009-02-21T05:37:57.537 回答
1

在这里乱砍一刀,这就是你想要的吗?

#! /usr/bin/perl

use strict;
use warnings;

while (<DATA>)
{
  if (/^(\w+) \| (\w+)$/     ||
      /^\[(\w+)\] \. (\w+)$/ ||
      /^(\w+)-(\w+)$/)
  {
    print "tag=$1, name=$2\n";
  }
}

exit 0;

__DATA__
team1 | foo
team1 | bar

[another] . user
[another] . player

more-james
more-brown

因为它生成:

tag=team1, name=foo
tag=team1, name=bar
tag=another, name=user
tag=another, name=player
tag=more, name=james
tag=more, name=brown
于 2009-02-21T04:01:55.943 回答
1

编辑:重新阅读问题和评论..

这适用于示例,但可能不适用于带有空格或标点符号的名称,以及可能的其他情况:

while ( <DATA> )
{
    if ( /(\w+).*?(\w+)$/ )
    {
        print "$1, $2\n";
    }
}


__DATA__
team1 | foo
team1 | bar

[another] . user
[another] . player

more-james
more-brown

给出:

team1, foo
team1, bar
another, user
another, player
more, james
more, brown
于 2009-02-21T04:42:28.497 回答
0

如果您一次只对一个玩家的名字运行正则表达式,我建议:

/(\w+)\W+(\w+)$/

在英语中,这意味着“至少一个单词字符,后跟至少一个非单词字符,后跟至少一个单词字符,然后是行尾”

“单词字符”是字母、数字和下划线。因此,如果人们在他们的标签/昵称中使用了这些字符以外的任何内容,则需要对其进行修改。例如,如果人们的昵称中可能还有连字符,您需要:

/(\w+)\W+([\w-]+)$/

据我所知,人们总是使用标点符号(和/或空格)来区分他们的氏族和他们的昵称,所以 \W+ 应该没问题。

至于你给出的没有分隔符的情况(clanplayer1,clanplayer2),如果不查看你知道在同一个氏族中的多个玩家的名字,并弄清楚他们的名字从什么时候开始不同,就没有办法解决这个问题,所以它不能单独使用正则表达式来解决。

于 2009-02-21T05:24:36.837 回答