Skip to content Skip to sidebar Skip to footer

Find The Sub-string After Nth '|'

Given a string as follows: 1|2||||auc|0|1||0|||76u| ^ what is the most efficient way to return the substring after the 5th '|'? For example, given the above string, the res

Solution 1:

Use str.split:

s = '1|2||||auc|0|1||0|||76u|'print s.split('|', 5)[-1]
# auc|0|1||0|||76u|

Note, this will cause possibly undesired results if there's not at least 5 |s, eg,

'1|2'.split('|', 5)[-1]
# returns 2 - which isn't *after* the 5th

present in the string, so you may wish to wrap it in a try/except and force handle the case where there aren't sufficient |s so that the result after the 5th is empty as there wasn't 5 present.

try:
    rest = s.split('|', 5)[5]
except IndexError:
    rest = ''

Solution 2:

Use the str.split() method with a limit (second argument):

input_string.split('|', 5)[-1]

This splits the string 5 times, and takes the last element, which has the remaining | charaters unsplit.

If there are fewer than 5 | characters in the string, you still will get the last element from the split, because [-1] counts from the end. This means the expression keeps working even if there are zero pipe symbols in the split.

Demo:

>>> input_string = '1|2||||auc|0|1||0|||76u|'>>> input_string.split('|', 5)
['1', '2', '', '', '', 'auc|0|1||0|||76u|']
>>> input_string.split('|', 5)[-1]
'auc|0|1||0|||76u|'

and quoting the documentation:

If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements).

Solution 3:

def get_substring(my_string):
    count =0for i, char in enumerate(my_string):
        if char == '|':
            count += 1if count == 5:
                return my_string[i+1:]

Solution 4:

s ='1|2||||auc|0|1||0|||76u|'
sre =  re.compile('([^|]*)('+ r*4+')(.*)')

sre.search(s).groups()
Out[39]: ('1', '|2|||', '|auc|0|1||0|||76u|')

sre.search(s).group(3)[1:]
Out[40]: 'auc|0|1||0|||76u|'

Post a Comment for "Find The Sub-string After Nth '|'"