Regular Expression help...!

Status
Not open for further replies.
I'm a beginner at regexp and I'm trying to parse this number filter that I grabbed from some web page. It seems to work but I'm trying to figure out why.

($_ =~ /^[\+-]?[0-9]*\.?[0-9]*$/ && $_ !~ /^[\. ]*$/)

The first part seems pretty clear but why do you have to backslash the "+" but not the "-"?

And what does the second part do? It's making sure that $_ doesn't match a "." that's beginning anchored followed by a space that's ending anchored?

Thanks.
 
I'd first assume the regexp is between / and / and the rest is language specific (looks like perl's default variable).

+ and . are operators, - has one special case within brackets. The backslash may or may not be required, but it's always best to err on the side of being explicit.

Here's my first guest:
(start of string)
(one or zero positive/negative symbols)
(zero ore more numbers in front of an optional decimal point)
(zero ore more numbers)
The second part likely means it must also not start with a decimals and/or spaces

What it all tells me is that it is done wrong since the second part is not explicitly stated in the first expression. The moral of the story is to not use code you don't understand.

I'd use something like:
Code:
[\+-]?[0-9]+(\.?[0-9]+)?
Here's hoping that it's not wrong. My intent was to state: an optional single unary operator, at least one number, followed optionally by a single decimal that requires at least following one number.

Example forms:
Code:
+0
-0
0
+0.0
+00.00
-000.000
0.0
00.00

+.0, 0., .0 are not allowed forms.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Your regex is still too complicated, and wrong (you did not surround it with ^ and $):
Code:
$ perl
$_ = '0.00.3';
print "\nnumber!\n"  if /[\+-]?[0-9]+(\.?[0-9]+)?/;
^D
number!
Remember that strings get malformed when being used in math. Strip out leading + signs and then do a test. In most cases, an equation is going to be less intensive than a regex, too.
Code:
my @num = (0, 0.000, +0, +00.00, 00.00, 'one', 'two', 1.2345, '1.two', '.002', '0.002');
for my $num (@num) {
    no warnings;
    $num = eval $num;
    print "'$num' is zero\n" if ($num eq '0');
    print "'$num' is a number!\n" if ( ($num + 0) eq $num);
}



'0' is zero
'0' is a number!
'0' is zero
'0' is a number!
'0' is zero
'0' is a number!
'0' is zero
'0' is a number!
'0' is zero
'0' is a number!
'1.2345' is a number!
'0.002' is a number!
'0.002' is a number!
You can combine the checks or short-circuit the control structure if you find a zero. There is one difference between this and the *intent* of the original regex, and that is that leading decimals are allowed. Personally I believe that is most likely a bad piece of logic, but you could restore that "feature" by changing the eval line to "$num =~ s/^\+//;". And of course, assign into a new var if you need the original input stored, though in many cases the conversion will happen at the time of input anyway.
 
ronelson":1usjg9pl said:
Your regex is still too complicated, and wrong (you did not surround it with ^ and $):

You're implementation is flawed, not the fact that my expression isn't designed for filtering [or, at least that's the way Conan O'Brian would explain the mistake].

--

It's worth repeating that using the languages built in function for dealing with input is going to be far more readable and maintainable than a regex. In this case, I'm assuming looks_like_number is what should be used and not some implicit language quirks.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
It's worth repeating that using the languages built in function for dealing with input is going to be far more readable and maintainable than a regex. In this case, I'm assuming looks_like_number is what should be used and not some implicit language quirks.
That may have a problem, though:
Code:
sub looks_like_number {
  local $_ = shift;

  # checks from perlfaq4
  return 0 if !defined($_) or ref($_);
  return 1 if (/^[+-]?\d+$/); # is a +/- integer
  return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
  return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);

  0;
}
Depends on the usage. Do you want someone to be able to put "Infinity" into your billing app's inputs? Either way, the regex solutions are definitely wrong.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Not to mention that localising a regex is a PITA.

Decent languages have this built into their frameworks. Yes they are a PITA in terms of speed if you're parsing structured data. But for unstructured you should just use it.

Not to mention if you plan on actually *parsing it* on languages that do not default to bigint. Good luck with 2147483648 or 9223372036854775808
 
Status
Not open for further replies.