-2

I am looking to extract numbers in the format:

[number]['/' or ' ' or '\' possible, ignore]:['/' or ' ' or '\' possible, ignore][number]['/' or ' ' or '\' possible, ignore]:...

For example:

"4852/: 5934: 439028/:\23"

Would extract: ['4852', '5934', '439028', '23']

user1581390
  • 1,900
  • 5
  • 25
  • 38

2 Answers2

0

Use re.findall to extract all occurrences of a pattern. Note that you should use double backslash to represent a literal backslash in quotes.

>>> import re
>>> re.findall(r'\d+', '4852/: 5934: 439028/:\\23')
['4852', '5934', '439028', '23']
>>>
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Python does have a regex package 2.7, 3.*

The function that you would probably want to use is the .split() function

A code snippet would be

import re
numbers = re.split('[/:\]', your_string) 

The code above would work if thats you only split it based on those non-alphanumeric characters. But you could split it based on all non numeric characters too. like this

numbers = re.split('\D+', your_string)

or you could do

numbers = re.findall('\d+',your_string)

Kudos!

Vincent Pakson
  • 1,891
  • 2
  • 8
  • 17