6

I am trying to split a string on any char that is not a digit.

orig = '0 1,2.3-4:5;6d7'
results = orig.split(r'\D+')

I expect to get a list of integers in results

0, 1, 2, 3, 4, 5, 6, 7

but instead I am getting a list with a single string element which matches the original string.

myol
  • 8,857
  • 19
  • 82
  • 143
  • Possible duplicate of [Python split string based on regex](https://stackoverflow.com/questions/13209288/python-split-string-based-on-regex) – Tim Biegeleisen Feb 17 '19 at 13:59

3 Answers3

9

Well ... you are using str.split() - which takes characters to split at - not regex. Your code would split on any '\D+' - string inside your text:

orig = 'Some\\D+text\\D+tosplit'
results = orig.split(r'\D+')  # ['Some', 'text', 'tosplit']

You can use re.split() instead:

import re

orig = '0 1,2.3-4:5;6d7'
results = re.split(r'\D+',orig)
print(results)

to get

['0', '1', '2', '3', '4', '5', '6', '7']

Use data = list(map(int,results)) to convert to int.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    If you want to get non-digit and digit sections, you can add group to regex: `re.split(r'(\D+)', orig)` – rysson Jul 15 '20 at 13:26
1

Try this:

orig = '0 1,2.3-4:5;6d7'
[i for i in orig if i.isdigit()]
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
0
for i in '0 1,2.3-4:5;6d7':
    try:
        print(int(i),end=' ')
    except:
        continue

0 1 2 3 4 5 6 7

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38