regex - Matching floating point numbers >= 100 -
i'm reading file line line , want lines starting rather ab , containing (at least) 3 digit floating point number followed percentage sign (i.e. floating point number% greater or equal 100%).
for instance:
aa whatevs 102.342% dontcare #match ab whatevs 102.342% dontcare #mismatch ac whatevs 12.3042% dontcare #mismatch * ad whatevs 102% dontcare #match * ae whatevs 2002.3042% dontcare #match af whatevs 22.3021% dontcare #mismatch ag whatevs 102.342 12.342% dontcare #mismatch **
i have following regex solution far, not capture ad whatevs 102% dontcare
obvious reason.
/^(?!ab).*\d{3}\.\d*%/
/^(?!ab).*\d{3}\.?\d*%/
not work either, since matches af whatevs 22.3021% dontcare
.
i know can splitting regex 2 sub regex based on existence of .
. however, i'd see if there single regex solution.
as you've noticed, there lot of different ways represent floating point numbers. can use regexp::common::number
handle them instead of rolling own regex:
use strict; use warnings; use regexp::common qw(number); while (<data>) { next if /^ab/; print if /\b$re{num}{real}{-keep}%/ , $1 >= 100; } __data__ aa whatevs 102.342% dontcare #match ab whatevs 102.342% dontcare #mismatch ac whatevs 12.3042% dontcare #mismatch * ad whatevs 102% dontcare #match * ae whatevs 2002.3042% dontcare #match af whatevs 22.3021% dontcare #mismatch ag whatevs 102.342 12.342% dontcare #mismatch ** ah whatevs 1.02342e02% dontcare #match ai whatevs -102% dontcare #mismatch
output:
aa whatevs 102.342% dontcare #match ad whatevs 102% dontcare #match * ae whatevs 2002.3042% dontcare #match ah whatevs 1.02342e02% dontcare #match
i've added negative number , exponent data set demonstrate convenience of using regexp::common
. whether data set includes such values or not, don't have tweak regex.
also note it's easier compare numbers when treat them numbers, not series of characters. -100
three-digit number, it's not >= 100
.
Comments
Post a Comment