Quote:
Originally posted by bishal
I have another question. How to ignore the last line also in perl. Example:
$line= /# milk in liter : 10.12[ltr]/;
I want only "10.12"
How to do that?
|
Code:
#!/usr/bin/perl -w
$str = '# milk in liter: 10.12[ltr]';
if ( $str =~ /^.*:\s([0-9\.]+).*$/ ) {
print( "$1\n" );
}
"^" matches the beginning of the line.
".*" matches zero or more of any character ("." matches any character, and "*" tells how many of them to match.) In this case, it matches "# milk in liter"
":\s" matches the colon followed by the space.
"([0-9\.]+)" is the key to the whole thing. Anything that matches the expression between "(" and ")" can be used later via the "$1" variable. See how we use it in the print statement? If you have multiple sets of "(" and ")" then you access them later via "$1", "$2", etc. The "[" and "]" represent a "character class". It means to match any of the characters between the "[" and "]". In this case we match any number 0 through 9, or the dot. The dot has to be escaped with a "\" otherwise it's the same as "any character". The "+" afterword means match one or more of the characters in the preceding character class. So, "[0-9\.]+" matches 10.12, 0.5, 10, ..1, 1.2.3.4, etc. We ignore the rest of the line by matching zero or more of anything, again using ".*".
"$" matches the end of the line.
If you wanted to capture the units (i.e. ltr) then you could do:
Code:
#!/usr/bin/perl -w
$str = '# milk in liter: 10.12[ltr]';
if ( $str =~ /^.*:\s([0-9\.]+).*\[(.*)\].*$/ ) {
print( "$1\t$2\n" );
}
We'll leave it as an exercise for the reader to figure out that regular expression.
Eric