Madhu
Madhu

Reputation: 97

String with no spaces needs to split based on pattern

I have a string

number234-456-132
abc235-456-456
bhjklsds:456-133-456

I want split the strings as

number 234-456-132
abc 235-456-456
bhjklsds: 456-133-456

There is no pattern to the text which is joined with the number.

Upvotes: 0

Views: 43

Answers (2)

Phydeaux
Phydeaux

Reputation: 2855

I would try explicitly to match the three groups of digits at the end, and include anything else in the first string:

for string in strings:
    match = re.match("(.*)(\d{3}-\d{3}-\d{3})$", string)
    print([match[1], match[2]])

Upvotes: 1

sanooj
sanooj

Reputation: 493

try this regex --> '([^0-9]*)(.*)'

>>> import re
>>> def foo(text):
...  result = re.search('([^0-9]*)(.*)', text)
...  return " ".join(result.groups())
... 
>>> foo("number234-456-132")
'number 234-456-132'
>>> foo("abc235-456-456")
'abc 235-456-456'
>>> foo("bhjklsds:456-133-456")
'bhjklsds: 456-133-456'
>>> 

Upvotes: 1

Related Questions