简体   繁体   English

正则表达式模式-使用Perl忽略选项卡和grep单词

[英]Regex Pattern - Ignore tab and grep word alone using Perl

Input File:(all are tab separated) 输入文件:(全部用制表符分隔)

abc   S12GG    HLPC
        WT4E    dfs.com   512
        SDA     djkf.com    1
        SWEW       abc.com    1
        SEFAW    dfsga.com    1
zyx   S12YT    TYSX
        wureyu    dfs.com   23
        ASWE     djkf.com    10
        werse       abc.com    16
        SDSDFS   dfsga.com    19

I am creating a hash table with the first line as one key and in the second line, just the first word as key. 我创建的哈希表的第一行是一个键,第二行是第一个单词作为键。 Below is the code: 下面是代码:

sub readFile {
    my ($fileName, $hash) = @_;
    my $lines=0;
    my $key;
    my $buffer;

    open (INPUT, $fileName);
    while($buffer=<INPUT>) {
        $lines++;
        if ($buffer=~/^(.*)\t(.*)\t(.*)$/) {
            $key=trim($1).";".trim($2).";".trim($3).";";
            $buffer=<INPUT>;
            $lines++;
        }
        $buffer=~/\t(.+)\t(.+)\t(.+)/;
        my $item=trim($1);
        my $group=trim($2);
        my $colinfo=trim($3);
        $hash->{$key}{$item}=["$group","$colinfo"];
    }
    close (INPUT);

    return $lines;
}

But this one matches both the lines in the if condition: 但这与if条件中的两行都匹配:

if ($buffer=~/^(.*)\t(.*)\t(.*)$/)

This matches both 这两者都匹配

abc   S12GG    HLPC
        WT4E    dfs.com   512

Can the if condition match only the first line?? if条件只能匹配第一行吗? I am really stuck on this and breaking my head for a long time. 我真的很固执,很长一段时间都无法接受。

https://regex101.com/r/v6JuDb/1/ https://regex101.com/r/v6JuDb/1/

I tried to use it for help. 我试图用它来寻求帮助。 But couldn't find any solution. 但是找不到任何解决方案。 Help would be appreciated. 帮助将不胜感激。 Thanks. 谢谢。

The way I'd do it: 我会这样做的方式:

chomp($line);
if (!/^\t/) {
    $key = $line;
    next;
}

my (undef, $item, $group, $colinfo) = split(/\t/, $line);
$hash->{$key}{$item} = [ $group, $colinfo ];

Or if the key format actually matters, 或者,如果密钥格式确实很重要,

chomp($line);
my @fields = split(/\t/, $line, -1);
if ($fields[0] ne "") {
    $key = join(';', @fields);
    next;
}

my (undef, $item, $group, $colinfo) = @fields;
$hash->{$key}{$item} = [ $group, $colinfo ];

请使用([^\\t]+)而不是(.*) ,这样它就不会跨TAB定界符匹配,并且必须匹配至少一个非TAB字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM