请帮忙
我正在使用一个文件,其数据行如下所示。可以看出,数据按'|||
'分为4,所以我将有四个数组(如果我将它除以)。我想要的是这个:
我的问题是,我记不起比赛了,它的位置!你能在这帮忙吗?
útil por la unión europea , a ||| by the european union , ||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)
答案 0 :(得分:5)
#!/usr/bin/perl
use strict;
use warnings;
my $string = "foo bar baz";
if ($string =~ /(foo) (bar) (baz)/) {
print "the whole match is between $-[0] and $+[0]\n",
"the first match is between $-[1] and $+[1]\n",
"the second match is between $-[2] and $+[2]\n",
"the third match is between $-[3] and $+[3]\n";
}
答案 1 :(得分:4)
pos()
功能可用于报告匹配的(结束)位置。例如:
my $string = 'abcdefghijk';
if($string =~ /e/g)
{
print "There is an 'e' ending at position ", pos($string), ".\n";
}
此代码将打印,“位置5处有'e'结尾。” (位置从0开始。)将此与捕获括号的正常使用相结合,您应该能够解决问题。
除pos()
外,还有特殊的全局数组@-
和@+
,它们提供匹配的每个子模式的起始和结束偏移。例如:
my $string = 'foo bar baz';
if($string =~ /(foo) (bar) (baz)/)
{
print "The whole match is between $-[0] and $+[0].\n",
"The first match is between $-[1] and $+[1].\n",
"The second match is between $-[2] and $+[2].\n",
"The third match is between $-[3] and $+[3].\n";
}
答案 2 :(得分:1)
当你在代码中做一些不简单的事情时,最好把它分解成离散的步骤和变量,以便它易于理解。
所以我首先将数据字符串分成四个部分:
#The data record
my $dataRec = "útil por la unión europea , a ||| by the european union , ||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)";
#split it into four parts
my ($Native, $English, $data1, $data2) = split(/\|\|\|/,$dataRec);
#Store the position of the punctuation mark
my $puncPos = index($Native, ",");
#If we found the punctuation mark, parse the data
my @dataList;
my $dataValue;
if ( $puncPos != -1 )
{
@dataList = split(/[)( ]/,$data1);
# use the punctuation position as the index into the array of values parsed
$dataValue = $dataList[$puncPos];
}
像这样......