12

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!

10
  • 1
    Do you want to match 1.23? What about .3? Commented Jan 26, 2010 at 13:54
  • If your input contains multiple matches, do you want all of them? Could your input contain anything other than dots, digits and spaces? Commented Jan 26, 2010 at 14:03
  • Thanks for updating your question with clarifications, what about 1.23 though? Commented Jan 26, 2010 at 14:08
  • ronik: Should it match if the input string is a1.2? Should I get 1.2 or no match? Commented Jan 26, 2010 at 14:09
  • What about 1.2, 3.4: should it match 1.2 and 3.4 or only 3.4 because of the comma after 1.2? Commented Jan 26, 2010 at 14:14

5 Answers 5

12

To match a json number:

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

JSON number

Use this regex to match .123:

^[-]?((0|[1-9][0-9]*)(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?$
Sign up to request clarification or add additional context in comments.

2 Comments

This example does not allow for .5. This 'number' is JSON-specific.
Here is an expression that will also allow for leading decimal points: [+\-]?(?:(?:\d+)(?:\.\d*)?|(?:\.\d+)+)(?:[eE][+\-]?\d+)?. Basically it says "either you have a digit then you can optionally add a decimal point and more digits, or you start with a decimal point, in which case you must have at least one digit".
10

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.

4 Comments

is ?: modifier valid on all platforms?
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.
@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.
@ronik: I've updated my answer again based on your recent clarifications. Sadly the ?: is back. Remove it if you don't need it.
7

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.

1 Comment

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

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

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

3 Comments

He doesn't seem to want to match 1.23. (Look at his original regex)
Maybe the poster doesn't want to match 1.23? I'll ask him to clarify this point.
it will work only if input string has one number... what about 1 3.5.4 10
1

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.

3 Comments

I know that I can use (i use c#), but i asked about pure regex
-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: Before his edit, regexps were wrong for this. Unless you really need to considers the contexts for the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.