Regular Expression - Numbers only, no space

Asked

Viewed 18,768 times

1

I have the following regular expression '((?:[\d][\s]?){5}[\d])' and I’m testing her on https://regex101.com/.

My problem is this: If I have that bit 'teste 123456 teste' she will return me only '123456' what is correct. But if it is 'teste 12 345 6 teste' she returns to me '12 345 6'.

I would like to know a way for it returns only the numbers without the spaces, if there are spaces.

  • Is there any restriction on the amount of digits you want? That {5} suggests that yes. If there is, what exactly would be this restriction?

  • I don’t know what language you’re using, but if it’s Javascript you can do the reverse process. Example: 'test 12 345 6 test'. replace(/[ d]+/g, '')

  • Just to clarify, what is to return if the input value is teste 1.324,33 manamana 52 ?

  • Or if the value is teste 12 345 xxx 6 teste? He should return 123456 or should reject because of xxx?

3 answers

3

Remove everything except numbers

"teste 123456 teste".replace(/\D/g, '');   // 123456
"teste 12 345 6 teste".replace(/\D/g, ''); // 123456

Capture all that is number and space, but consider the numbers

var input = "teste 12 345 6 teste"; // string teste
var regex = /(\d+)| /g;             // regex

var matches, output = [];           // vars para processo
while (matches = regex.exec(input)) {  // captura do contudo, o exec vai capturar 
                                       // o primeiro resultado que encontrar seja 
                                       // `\d` ou ` `, quando capturar ` ` não 
                                       // haverá grupo 1, assim ao fazer o `matches[1]` 
                                       // este estará `undefined` que no filter é 
                                       // false, assim o eliminando do array.

    output.push(matches[1]);        // adiciona o grupo 1 a out
}
output.filter(function(value){
  return value;                     // limpeza do array
}).join('')                         // concatena tudo por join

Upshot : 123456

  • Oops: "teste 12 345 xxx 6 teste"

  • @Victorstafusa the result was the same, what you wish you know?

  • It is because in this case, he SHOULD NOT accept this, since the six numbers are not just separated by spaces.

  • Sorry, I forgot something here :S

  • In my test I put the g, when rewriting here I forgot.

  • The problem isn’t just the g. The problem is finding six numbers in a row separated by spaces and not six numbers in a row separated by anything.

  • @Victorstafusa then would have to ask the author how he wants, because for what I saw in the question, can not be sure what is best for him if it is teste 1.234.45 manamana 4, for example. I left a comment on the question to see if we can get more details.

Show 2 more comments

2

If it is exactly for this expression that says in the commentary, do so as it is. If you put the possible variables, which agent gets it right...

"(\b[0-9]\b)/g"

If it’s enough of the ok in the answer, if no comments which agent hits...

  • So Magichat... so it doesn’t work... if my string is in this format 'test 1 2 3 4 6 test2 Edf 4452 rdf' this expression won’t bring me the sequence if 6 who can or can’t is with space (it will bring all the numbers). I want her to bring me only the six numbers in this sequence, but bring me without the spaces...

  • Luis, if you are using regex101.com, remember that the /g goes in the other field of the expression ... I recommend using the regexpal.com

2

Regular expressions serve to recognize characters within a string. With them you can also obtain which substring has been recognized.

It turns out that 123456 is not substring of teste 12 345 6 teste. And therefore, you won’t get 123456 as a response using only regex in a single step because 123456 is not exactly like this in the input string.

So you do this:

  1. Uses the ((?:[\d][\s]?){5}[\d]) to locate the number with the spaces.
  2. Strip the spaces later using a replace.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.