-2

I have an example string like this:

1 23 456 123asd asd123 12 3456789

I want to create a regex to match all only number tokens, so that it would match with 1, 23, 456, 12, and 3456789.

I already tried to create the regex which is \b[^a-zA-Z\s]\d*[^a-zA-Z\s], but somehow it only matches with 23. It won't match with 1, and all the other number tokens after 23.

What am I doing wrong?

  • Are you expecting this to match *all* number tokens? With a single run of digits? – Scott Hunter Sep 24 '20 at 13:00
  • I did. I thought that by using regex pattern I could match with multiple values. Turns out that what I need is a function such as `findall` from `re` module, as said by @Joshua Fox in the answers section. – Ahmad Naufal Hakim Sep 24 '20 at 13:45

4 Answers4

0

The simple regex \d+ will do this. And you want an API that returns multiple values, for example findall.

import re
found = re.findall(r'\d+', '1 23 456 123asd asd123 12 3456789')
print(found)

outputs

['1', '23', '456', '123', '123', '12', '3456789']
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
  • Oh wow thank you very much for your answer. I've been looking everywhere for such regex pattern but can't find it. I really thought I can use a single regex to match all the numbers. I didn't know that I just need to use a single function. Thanks Joshua – Ahmad Naufal Hakim Sep 24 '20 at 13:39
0

\b\d+\b gets only numbers and no letters concatenated

woblob
  • 1,349
  • 9
  • 13
0

The \b assert already match word boundary, so there no need to rematch with [^a-zA-Z\s]. Besides, this match list doesn't include start or end of match, so "1" and "3456789" are not matched.

\b[0-9]+\b will be enough to match only numeric tokens : it match groups of digits not inside another bloc (thanks to word boundary assets)

darkelfe
  • 35
  • 1
  • 8
0

Another simple solution is [^\D]+

import re
found = re.findall('[^\D]+',"this is test #23 1 23 456 123asd asd123 12 3456789")
print(found)

['23', '1', '23', '456', '123', '123', '12', '3456789']
javadr
  • 310
  • 2
  • 12