I asked in another topic about matching numbers like 123. This was too narrow and as I get deeper into Regex I see that you really have to define anything. So I asked for exponential notation and got an answer in this post: /^keyword\s+(-?(?:\d+|\d*\.\d*)(?:[Ee]-?(?:\d+|\d*\.\d*))?)/
. I tried to understand this but failed so far.
So I ask more specific now. I need to match numbers, I give some examples here:
13
-999
83.12300
.151
-.213
1e14
124e2
-9e-4
You got it, the regular math stuff.
And to be even more specific I give you my Perl code for this. I am searching for keyword
on a line and need to get a value from this line. I'd like to get this value in one Regex because my workaround with the or-statement ||
seems to cause problems.
my $value;
open(FILE,"data.dat") or die "error on opening data: $!\n";
while (my $line = <FILE>) {
if (($line =~ /^keyword\s+(-?(?:\d+|\d*\.\d*)(?:[Ee]-?(?:\d+|\d*\.\d*))?)/x) || ($line =~ /^keyword\s*(\d*\.\d*)/)) {
$value = $1;
};
}
close(FILE);
Edit
Thx to all for the hints so far.