perl - How to make regex accept strings with a dot -
i'm debugging perl script , encountered errors described in following sections below:
in script, have -td
variable designed accept string 1n, 5n, 0.3n, 0.8n
but when try use the last 2 said list, script not work intended, , works fine when use first 2 list.
to give overview, have written portions of script, after code, state concern:
if (scalar(@argv) < 1){ &get_usage() }; # getoptions setup ## getoptions ( 'h|help' => \$help, 'v|version' => \$version, 'i|input=s' => \$input, 'o|output=s' => \$output, ... # more options here 'td=s' => \$td_val, # line in question 'val=s' => \$v_var, ... # more options here ) or die get_usage(); # call usage of script or ... # more codes here get_input_arg(); # function validate entries user had inputted #assigning placeholder value creating new file $td_char="\ttd=$td_val" if $td_val; $td_char=" " if !$td_val; ... # fast forward ... sub get_input_arg{ ... # here go, there wrong in following regex accept values such 0.8n unless (($td_val=~/^\d+(m|u|n|p)s$/)||($td_val=~/^\d+(m|u|n|p|s)$/)||($td_val=~/^\d+$/)){# print "\n-td error!\nenter required value!\n"; get_usage(); ... # more functions here ... }
for explanation:
- on console user input
-td 5n
- the
5n
assignedtd_val
,td_char
, used printing later - this
5n
validatedget_input_arg()
function pass regexunless
line.
for 5n
input, script work intended, when use -td 0.8n
, after validating it, print error message after unless
line on console
i know regex failing on matching regards using 0.8n
td
input, don't know how can fix it. in advance!
you can use
unless (($td_val=~/^\d+(\.\d+)?[munp]s$/)||($td_val=~/^\d+(\.\d+)?[munps]$/)||($td_val=~/^\d+(\.\d+)?$/))
explanation:
your regex has \d+
matches integers.. replace \d+(\.\d+)?
(integral part followed optional decimal part)
see demo
Comments
Post a Comment