Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda