tags:

views:

159

answers:

4

I have this pattern:

[0-9]*\.?[0-9]

It matches numbers but it also matches 3.5.4 as:

  1. 3.5
  2. .4

How to fix that(this input shouldn't be matched)?
UPDATE:
this also should work on input: 1 3.5.4 10

1.2. is not allowed
.3 is allowed

any char that is not poart of numer is not allowed, like: a1 2.4f 3. 45, 67!

+3  A: 

Your regex is not anchored. If you want to match lines that contain only numbers and nothing else use:

^[0-9]*\.?[0-9]$
schot
This will fail on 1.23. Why is this getting upvoted?
cletus
He doesn't _seem_ to want to match `1.23`. (Look at his original regex)
SLaks
Maybe the poster doesn't want to match `1.23`? I'll ask him to clarify this point.
Mark Byers
it will work only if input string has one number... what about 1 3.5.4 10
ronik
+3  A: 

Updated answer after comment from poster:

Use lookahead / lookbehind to make sure that the characters before and after are spaces:

Here's a version that closely matches yours, but that won't make partial matches:

(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)

For both these examples, when run on the string 1 2.3.4 5.6 it matches only 1 and 5.6.

Mark Byers
is ?: modifier valid on all platforms?
ronik
Most, yes. What platform are you using? My current solution doesn't use `?:` anyway. And even if it did it's always optional - it just improves performance. So if your platform doesn't support it, just remove it.
Mark Byers
@ronik: I think my answer is nearly what you want, but you need to answer my clarifying questions before I can make something that is exactly what you need. See the comments under your question and either reply to them, or update your question to include the answers.
Mark Byers
@ronik: I've updated my answer again based on your recent clarifications. Sadly the `?:` is back. Remove it if you don't need it.
Mark Byers
I guess this is good enough.so I accept it.thank you
ronik
You're welcome.
Mark Byers
A: 

You should not use a regex for this unless you really need to.

Instead, you should use your language's built-in number parsing methods.

I assume from your other questions that you're using Javascript; if so, you should call parseFloat.

SLaks
I know that I can use (i use c#), but i asked about pure regex
ronik
-1 for assumption that regexps are wrong. Alternative suggestions are one thing, but bland statements such as "You should not use a regexp for this unless you really need to" are inflammatory and do not consider the possible contexts for the question.
PP
@PP: Before his edit, regexps were wrong for this. `Unless you really need to` considers the contexts for the question.
SLaks
+5  A: 

You have to decide if you want to accept numbers without leading zeros (eg .123). If you don't then the regex is easy:

^-?[0-9]+(\.[0-9]+)?$

If you do then it's a little more complex:

^-?(?:[0-9]+|[0-9]*\.[0-9]+)$

Both presume that a decimal point must be followed by at least one digit. If you don't accept negative numbers then the leading -? is unnecessary.

cletus
@cletus: I recalculated your rep, as requested. It changed such a tiny amount, I thought I'd better let you know. :)
Bill the Lizard