我有一个格式低于文件的文件,我需要使用Perl和regex来获取插入数据库的值。我尝试使用正则表达式,但我有错误。按照我用来获取第一个值的脚本:
# line 1
open(my $fin, "<", "input.txt")
or die "cannot open < input.txt: $!";
my $line1 = <$fin>;
chomp $line1;
print "$line\n";
my ($codigo, $nome) = ($line1 =~ m/^((\d)+)\s[\S]\s(([\s\d\w])+)$/);
print "#$codigo#$nome#\n";
# line 2
$line2 = <$fin>;
chomp $line2;
..
但是我没有看到“codigo”和“nome”的正确价值。
文件格式为:
84404167 - NAME ONE OF SILVA R NONONONO, 143334, HOUSE - REGION - CITY - 81280400 Res: (22)5555.4543 Cel: (33)5555.8659 Ou: Início: 17/12/2013 - Data de aniv.: 23/02/1955 Crédito: 1440 - Crédito Disponível: 1152 - Status: ATIVA 96071311 - NAME SECOND OF JOSE R SECRET ADDRESS NONONNO, 433, ap 232 b azaleia - fazendinha - CURITIBA - 81320420 Res: Cel: (22)5555.9776 Ou: (33)5555.2352 Início: 22/01/2015 - Data de aniv.: 10/05/1981 Crédito: 764 - Crédito Disponível: 516 - Status: ATIVA
答案 0 :(得分:1)
您可以使用split
函数代替正则表达式:
#!/usr/bin/perl
use warnings;
use strict;
open my $fin, "<", "abc.txt" or die "cannot open input.txt: $!";
while (my $line1 = <$fin>)
{
chomp $line1;
if ($. == 1) # line 1
{
my ($codigo, $nome) = split (/\s*-\s*/, $line1);
print "codigo: $codigo nome: $nome\n";
}
}
输出:
codigo: 84404167 nome: NAME ONE OF SILVA