1

Hello iam trying to find all the degits in a string but it returns an empty list

my code :

import re
re.findall(r'/d','5585858')
>>>> []

what is the error please ?

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169

3 Answers3

2

You have a wrong pattern, you need \d first and for get all of digits you can add + to \d for match 1 or more combine of digits :

re.findall(r'\d+','5585858')

Also based on your string you can use other functions like re.search that may be more efficient.

And if you want to convert your string to list you can simply use list :

>>> list('5585858')
['5', '5', '8', '5', '8', '5', '8']
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

use '\d+' instead of '/d'

 >>> import re
 >>> re.findall(r'\d+', '5585858')
 ['5585858']
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
0

You don't even need a regular expression for that.

>>> [number for number in '5585a858' if number.isdigit()]
['5', '5', '8', '5', '8', '5', '8']
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97