Perl - get first “word” from input string

后端 未结 2 472
广开言路
广开言路 2021-01-11 15:44

I am trying to write a Perl program that reads in lines from a text file, and, for each line, extract the first \"word\" from the line, and perform a different action based

相关标签:
2条回答
  • 2021-01-11 16:09

    This will cover all of your cases and then some:

    my ($key, $value) = split /\s*:\s*/, $inputline, 2;
    

    (Or, in English, split $inputline into a maximum of two elements separated by any amount of whitespace, a colon and any amount of whitespace.)

    0 讨论(0)
  • 2021-01-11 16:20
    ($start) = $inputline =~ /\A([^:\s]+)/;
    

    This will match anything except whitespace and : at the beginning of the line.
    Or using split:

    ($start) = split /[:\s]+/, $inputline, 2;
    
    0 讨论(0)
提交回复
热议问题