split string on any special character using python

Question:

currently I can have many dynamic separators in string like

new_123_12313131
new$123$12313131
new#123#12313131

etc etc . I just want to check if there is a special character in string then just get value after last separator like in this example just want 12313131

Asked By: newbiee

||

Answers:

This is a good use case for isdigit():

l = [
'new_123_12313131',
'new$123$12313131',
'new#123#12313131',
]

output = []
for s in l:
    temp = ''
    for char in s:
        if char.isdigit():
            temp += char
    output.append(temp)        

print(output)

Result: [‘12312313131’, ‘12312313131’, ‘12312313131’]

Answered By: JacobIRR

Assuming you define ‘special character’ as anything thats not alphanumeric, you can use the str.isalnum() function to determine the first special character and leverage it something like this:

def split_non_special(input) -> str:
    """
    Find first special character starting from the end and get the last piece
    """
    for i in reversed(input):
        if not i.isalnum():
            return input.split(i)[-1] # return as soon as a separator is found
    return '' # no separator found

# inputs = ['new_123_12313131', 'new$123$12313131', 'new#123#12313131', 'eefwfwrfwfwf3243']
# outputs = [split_non_special(input) for input in inputs]
# ['12313131', '12313131', '12313131', ''] # outputs

Answered By: Deepak Gaur

Python supplies what seems to be what you consider "special" characters using the string library as string.punctuation. Which are these characters:

!"#$%&'()*+,-./:;<=>?@[]^_`{|}~

Using that in conjunction with the re module you can do this:

from string import punctuation
import re

re.split(f"[{punctuation}]", my_string)

my_string being the string you want to split.

Results for your examples

['new', '123', '12313131']

To get just digits you can use:

re.split("d", my_string)

Results:

['123', '12313131']
Answered By: Jab

just get value after last separator

the more obvious way is using re.findall:

from re import findall

findall(r'd+$',text)  # ['12313131']
Answered By: SergFSM
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.