Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Limit 10 characters is numbers and only 1 dot

I'm having a regex problem when input That's the requirement: limit 10 characters (numbers) including dots, and only 1 dot is allowed My current code is only 10 characters before and after the dot.

^[0-9]{1,10}\.?[0-9]{0,10}$

thank for support.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The decimal point throws a wrench into most single pattern approaches. I would probably use an alternation here:

^(?:\d{1,10}|(?=\d*\.)(?!\d*\.\d*\.)[0-9.]{2,11})$

This pattern says to match:

^                   from the start of the number
(?:
    \d{1,10}        a pure 1 to 10 digit integer
    |               OR
    (?=\d*\.)       assert that one dot is present
    (?!\d*\.\d*\.)  assert that ONLY one dot is present
    [0-9.]{2,11}    match a 1 to 10 digit float
)
$                   end of the number
about 4 years ago · Juan Pablo Isaza Report

0

You can use a lookahead to achieve your goals.

First, looking at your regex, you've used [0-9] to represent all digit characters. We can shorten this to \d, which means the same thing.

Then, we can focus on the requirement that there be only one dot. We can test for this with the following pattern:

^\d*\.?\d*$
  • \d* means any number of digit characters
  • \.? matches one literal dot, optionally
  • \d* matches any number of digit characters after the dot
  • $ anchors this to the end of the string, so the match can't just end before the second dot, it actually has to fail if there's a second dot

Now, we don't actually want to consume all the characters involved in this match, because then we wouldn't be able to ensure that there are <=10 characters. Here's where the lookahead comes in: We can use the lookahead to ensure that our pattern above matches, but not actually perform the match. This way we verify that there is only one dot, but we haven't actually consumed any of the input characters yet. A lookahead would look like this:

^(?=\d*\.?\d*$)

Next, we can ensure that there are aren't more than 10 characters total. Since we already made sure there are only dots and digits with the above pattern, we can just match up to 10 of any characters for simplicity, like so:

^.{1,10}$

Putting these two patterns together, we get this:

^(?=\d*\.?\d*$).{1,10}$

This will only match number inputs which have 10 or fewer characters and have no more than one dot.

If you would like to ensure that, when there is a dot, there is also a digit accompanying it, we can achieve this by adding another lookahead. The only case that meets this condition is when the input string is just a dot (.), so we can just explicitly rule this case out with a negative lookahead like so:

(?!\.$)

Adding this back in to our main expression, we get:

^(?=\d*\.?\d*$)(?!\.$).{1,10}$
about 4 years ago · Juan Pablo Isaza Report

0

You could assert 10 chars in the string being either . or a digit.

Then you can match optional digits, and optionally match a dot and again optional digits:

^(?=[.\d]{10}$)\d*(?:\.\d*)?$

The pattern matches:

  • ^ Start of string
  • (?=[.\d]{10}$) Positive lookahead, assert 10 chars . or digit till the end of string
  • \d* Match optional digits
  • (?:\.\d*)? Optionally match a `. and optional digits
  • $ End of string

See a regex demo.

If the pattern should not end on a dot:

^(?=[.\d]{10}$)\d*(?:\.\d+)?$

Regex demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!