Re: Regex to extract number from text file
by moritz (Cardinal) on Feb 18, 2009 at 10:11 UTC
|
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
| [reply] [d/l] |
|
|
Hi
Thanks a lot for your help. I will try your solution and you have pertactly understood my problem.
| [reply] |
|
|
Thanks for your reply. I tried writing following code but it is not printing anything on screen.
my @temp;
open(FILE,"C:\\test.txt");
@temp = <FILE>;
close(FILE);
foreach (@temp)
{
my $search =~ m/ClearQuest\: \(Total of 18 licenses issued\; Total of (\d+)/;
my $wd = $1;
print "\n$wd\n";
exit;
| [reply] |
|
|
That's a bit of surprise to me, because for me it prints
Missing right curly or square bracket at /home/moritz/foo.pl line 10,
+at end of line
syntax error at /home/moritz/foo.pl line 10, at EOF
Execution of /home/moritz/foo.pl aborted due to compilation errors.
Please use <code>...</code> tags around code examples that you put here, and also make sure to copy & paste them from your actual script to not introduce some bugs by incorrectly writing it down.
Also please start your scripts with
use strict;
use warnings;
And you'll get at least a warning about what you're doing wrong. | [reply] [d/l] [select] |
|
|
You need to change the code:
my $search =~ m/ClearQuest\: \(Total of 18 licenses issued\; Total of
+(\d+)/;
to:
(my $search)=$_=~ m/ClearQuest\: \(Total of 18 licenses issued\; Total
+ of (\d+)/;
| [reply] [d/l] [select] |
Re: Regex to extract number from text file
by velusamy (Monk) on Feb 18, 2009 at 10:39 UTC
|
while (<FH>) {
print "Matched Number:",$1 if(/(\d+)/g);
}
you will get the matched number in $1. | [reply] [d/l] |
|
|
use strict;
use warnings;
my $str = 'wibble 10';
print "Matched $1\n" if $str =~ /(\d+)/g;
print "Matched $1\n" if $str =~ /(\w+)/g;
Prints:
Matched 10
Because of the /g switch the second match fails because is starts searching from where the first match left off.
True laziness is hard work
| [reply] [d/l] [select] |
Re: Regex to extract number from text file
by hda (Chaplain) on Feb 18, 2009 at 12:09 UTC
|
Depending on the eventual design of your program and the format of your input file (for example, if the number you are looking for changes a lot in format as integer, floating point, etc), you might find useful the module: Scalar::Util.
Have a look at the following node:
Checking whether a value is numeric or not
| [reply] |
Re: Regex to extract number from text file
by irah (Pilgrim) on Feb 18, 2009 at 10:49 UTC
|
In every line in your file have only two digits, (here 12 is two digits), you can use one more way as,
[0-9]{2}
If the number of digits is not fixed, use,
[0-9]+
You can get to know from man pages. You can store the values using $1 variable. | [reply] [d/l] [select] |