2

I'm trying to "validate" some input from a form on the backend but I kinda got stuck with the required regex. Basically, I want to get a number, no leading zeroes, with an optional decimal and an optional case insensitive thousands notation (1,000 = 1k, 1,000,000 = 1m, 1 billion = 1b ...) These should match/validate:

12.39
12.389k
99.1003b
40.1m

These should fail.

0.0
12.00b
1e3
2^5
0xFF
12.6z
asdf

So far I've tried this:

^[1-9]\d*(?:\.\d+)?(?:[kmbt])?$

It works but I'm thinking maybe someone could solve this a bit more elegantly than I did.

6
  • 2
    Your regex fails to fail on 12.00b Commented Oct 14, 2016 at 12:43
  • Why should pass 99.1003b and fail on 12.00b ? Commented Oct 14, 2016 at 12:51
  • Your regex isn't taking into account thousands notation, is that something you care about? There are no examples in your examples but your description mentions it. or 40.1m is in your string but denotes 40,100,000? Commented Oct 14, 2016 at 12:53
  • @xpuc7o I think OP does not want trailing zeroes, too. Commented Oct 14, 2016 at 13:13
  • You can try with : regex101.com/r/VWbOQc/1 Commented Oct 14, 2016 at 13:31

3 Answers 3

1

[Edit] without thousand separator:

/^(?:[1-9][0-9]*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i

demo


[Old answer] with thousand separator:

You can do it with:

/^(?:[1-9][0-9]{0,2}(?:(,?)[0-9]{3})?(?:\1[0-9]{3})*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i

demo

details:

^
(?: # numbers >= 1
    [1-9][0-9]{0,2}
    (?:(,?)[0-9]{3})? # capture an eventual thousand separator
    (?:\1[0-9]{3})*
    # decimal
    (?:\.[0-9]*[1-9])?

  | # numbers < 1
    0\.[0-9]*[1-9]
)[kbmt]?$
Sign up to request clarification or add additional context in comments.

5 Comments

Thats a huge number of steps... and will only match on new line will it not?
@AlexanderHolman: the pattern is designed for a whole string (as the pattern example in the question). The goal is to valid, not to extract. The number of steps is low, about 20 for a valid string and about 25 for an invalid string, it's few.
the one without the thousand separator works like a charm. Thank you! Just can't really figure out why the one from @ShekharKhairnar link works
@Casimir et Hippolyte my bad, I missed the "validate"
@CasimiretHippolyte yep, just noticed that right now after a few test cases. Oh well, it was short and sweet xD
0

Update in REGEX.

Below regex will not leading zero allow special characters and characters without specified for notation.

Below regex will allow decimal and integer with thousand and other notation.

^[1-9]\d*(\.[0-9]+)?(?:[kmbtKMBT])?$

Please check working demo : http://www.regexpal.com/?fam=95995

1 Comment

This will match 12.00 which is not really desired. Thanks
0

The below should be what you're after:

/(\b[1-9][\d\,\.]+([kmbt]?))(\s)/g

It matches 12.00b however... Here is an example of it's use.

Group 1 will contain your exact match.

1 Comment

works, but as stated matches 12.00b which I'm trying to avoid altogether.

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.