darkzet
darkzet

Reputation: 3

Python How to split a string when find the first numeric char

This is my string: "Somestring8/9/0"

I need to get something like this: ['Somestring','8/9/0']

The moment I find a numeric char, I need to split the string to get: '8/9/0'

This my code:

stringSample = "GigabitEthernet8/9/0"
print re.findall(r'(\w+?)(\d+)', stringSample)[0]
('GigabitEthernet', '8')

But I'm getting this result What am I doing wrong?

I appreciate your help!!

Upvotes: 0

Views: 41

Answers (2)

brlaranjeira
brlaranjeira

Reputation: 367

Try Using the re.split method to split your string in two, passing the maxsplit parameter

re.split('(\w+?)([\d/]+)', stringSample, 1)

Upvotes: 0

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23223

Your second regex group accepts only digits. Allow it to include forward slashes too.

stringSample = "GigabitEthernet8/9/0"
print re.findall(r'(\w+?)([\d/]+)', stringSample)[0]
# ('GigabitEthernet', '8/9/0')

Upvotes: 1

Related Questions