Regex capturing exactly x digits of a string

6

I'm trying to make a regular expression where it exactly captures x digits (specifically in my case x = 6).

Example:

"test 1 n ° 123456 end of test"
"test 2 n ° 7890123 end of test"

I want to return only the "123456" in the regular expression, that is, only numbers that contain exactly 6 digits. How could I do it?

    
asked by anonymous 19.12.2018 / 22:38

3 answers

9

You can use the following regex:

\b\d{6}\b

The \d is the meta caratere for digit, searching for a digit from 0 to 9.

{6} indicates that you must find 6 of these in a row.

The \b indicates that it is the beginning and end of the word, which implies that the 6 numbers must be followed, making 7 or more numbers no longer valid.

See working on regex101

Works perfectly for the case you mentioned. If you need something larger that works even with the numbers that follow the text, you can use for example this regex, which is a bit more complex:

^|\D(\d{6})\D|$

Explanation:

^|\D    - Inicio de linha ou algo que não seja um digito
(\d{6}) - 6 dígitos seguidos e a serem capturados no grupo de captura 1
\D|$    - Seguido de algo que não seja um digito ou fim de linha

Here too, I used the% karatere% meta that is the inverse of \D , and means something other than a digit.

A subtle difference from this regex to the previous one is that in this the digit is in capture group 1, rather than in the complete capture of the regex. So regardless of how you use the regex in code, getting the number will always be different.

View on regex101

In regex territory it's always like this. The simpler general rule are more restricted and do not control every case, but sometimes they are exactly what you need. You must always try to find a balance between what you want to validate and the complexity / efficiency you are willing to use / abdicate.

    
19.12.2018 / 23:11
3

@Isac's great response that applies meta-character \b , however, using \b will only serve in your specific case. I would like to leave here a more generic alternative.

\b works for you because the 6 digits are preceded by a special ° character and succeeded by a space, ie, the catch string between two \b 's will only be found if it is between two special characters (except underlined _ ) or space.

For example, if the first string quoted in the question did not have ° before the number (so: n123456 ), the 123456 number would not be found with \b . See:

var str = "teste 1 n123456 fim do teste";
console.log(/\b\d{6}\b/.test(str));

This is because \b , as said, requires that the searched string be between special characters.

My suggestion is not the most elegant, but it will return the number (no matter where it is) that contains 6 digits.

See:

var string1 = "teste 1 n123456 fim do teste";
var string2 = "teste 2 n°7890123 fim do teste";

var teste1 = string1.match(/\d{6,}/);

if(teste1 && teste1[0].length == 6){
   console.log("'"+ teste1[0] +"' encontrado na string1");
}

var teste2 = string2.match(/\d{6,}/g);

if(teste2 && teste2[0].length == 6){
   console.log(teste2[0]);
}else{
   console.log("Nada encontrado na string2");
}

What does the code do? It looks for a number with 6 or more digits and then compares if that number is only 6 characters long; if it does, OK, otherwise it does nothing.

    
20.12.2018 / 02:34
0

Hello

You can use the following expression:

/\b\d{6}\b/g

UsetheregExrwebsitetotestyourcodes,theyhaveafairlycompletereference link

I hope I have helped! ^^

    
24.12.2018 / 13:51