我有一个包含这样字符串的文件:
- line 1: array[0]=' *[0-9]-? [^a-c]@[*-^a-c]'; array [1]='' < temp-test/758.inp.325.1
- line 2: array[0]=' *[0-9]-? [^a-c]@[*-^a-c]'; array [1]='' < temp-test/759.inp.325.3
- line 3: array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='NEW' < temp-test/1133.inp.487.1
- line 4: array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='NEW' < temp-test/1134.inp.487.2
- line 5: array[0]='"@@'; array[1]='m' < input/ruin.1890
我想将每行的这个字符串拆分成2部分,我希望结果如下:
#!/usr/bin/perl
# location of universe file
$tc = "/root/Desktop/SIEMENS/replace/testplans.alt/universe";
# open file universe;
open( F, "<$tc" );
@test_case = <F>;
while ( $i < 5 ) {
$test_case[$i] =~ s/ //;
@isi = split( / /, $test_case[$i] );
if ( $#isi == 2 ) {
print "Input1:" . $isi[0] . "\n";
print "Input2:" . $isi[1] . "\n";
print "Input3:" . $isi[2] . "\n";
}
$i++;
}
我尝试的代码是这样的:
tsconfig.json
我感到困惑,因为我不能用&#34; &#34; (空间),因为每一行都有不同的订单空间,我不能成为2部分。 谢谢。
答案 0 :(得分:0)
use strict;
use warnings;
# this stuff just gives me your data for testing
my $data =<<EOF;
' [0-9]-? [^a-c]\@[-^a-c]' '' < temp-test/758.inp.325.1
' [0-9]-? [^a-c]\@[-^a-c]' '' < temp-test/759.inp.325.3
' *[0-9]\@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1133.inp.487.1
' *[0-9]\@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1134.inp.487.2
'"\@\@' 'm' < input/ruin.1890
EOF
for my $line (split(/\n/,$data))
{
# this re splits your strings according
# to what I perceive to be your requirements
if ($line =~ /^(.*)('.*?' <.*)$/)
{
print("array[0]=$1; array[1]=$2;\n")
}
}
1;
输出:
array[0]=' [0-9]-? [^a-c]@[-^a-c]' ; array[1]='' < temp-test/758.inp.325.1;
array[0]=' [0-9]-? [^a-c]@[-^a-c]' ; array[1]='' < temp-test/759.inp.325.3;
array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='NEW' < temp-test/1133.inp.487.1
array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='NEW' < temp-test/1134.inp.487.2;
array[0]='"@@' ; array[1]='m' < input/ruin.1890;
应用于您的代码,它可能如下所示:
while ($i<5)
{
$test_case[$i] =~ /^(.*)('.*?' <.*)$/;
@isi = ($1,$2);
print "Input1:".$isi[0]."\n";
print "Input2:".$isi[1]."\n";
$i++;
}