Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - get first "word" from input string

Tags:

string

regex

perl

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 on the string that gets returned.

The main loop looks like this:

while(<AXM60FILE>) {

   $inputline = $_;

   ($start) = ($inputline =~ /\A(.*?) /);

perform something, based on the value of string in $start

}

The input file is actually a parameter file, with the parameter_name and parameter_value, separated by a colon (":"). There can be spaces or tabs before or after the colon.

So, the file looks (for example) like the following:

param1: xxxxxxxxxxxx
param2 :xxxxxxxxxxxxx
param3 : xxxxxxxxxxxxxxxxx
param4:xxxxxxxxxxxxx

That "($start) = ($inputline =~ /\A(.*?) /);" works ok for the "param2" example and the "param3" example where the 1st word is terminated by a blank/space, but how can I handle the "param1" and "param4" situations, where the parameter_name is followed immediately by the colon?

Also, what about if the "whitespace" is a tab or tabs, instead of blank/space character?

Thanks, Jim

like image 363
user555303 Avatar asked Feb 11 '11 19:02

user555303


2 Answers

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.)

like image 122
Blrfl Avatar answered Oct 31 '22 11:10

Blrfl


($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;
like image 27
Eugene Yarmash Avatar answered Oct 31 '22 11:10

Eugene Yarmash