Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string at nth occurrence of a given character

Is there a Python-way to split a string after the nth occurrence of a given delimiter?

Given a string:

'20_231_myString_234' 

It should be split into (with the delimiter being '_', after its second occurrence):

['20_231', 'myString_234'] 

Or is the only way to accomplish this to count, split and join?

like image 634
cherrun Avatar asked Jun 12 '13 07:06

cherrun


People also ask

How do you split a string at a certain character?

To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split a string on the first occurrence of certain characters?

To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.

How do you find the nth occurrence of a character in a string?

1) Select Lookup from the drop-down list of Formula Type section; 2) Choose Find where the character appear Nth in a string in Choose a formula section; 3) Select the cell which contains the string you use, then type the specified character and nth occurrence in to the textboxes in the Arguments input section.

How do you split a string on second occurrence of character?

a. split("-", 2) will split the string upto the second occurrence of - . a.


2 Answers

>>> n = 2 >>> groups = text.split('_') >>> '_'.join(groups[:n]), '_'.join(groups[n:]) ('20_231', 'myString_234') 

Seems like this is the most readable way, the alternative is regex)

like image 174
jamylak Avatar answered Oct 05 '22 23:10

jamylak


Using re to get a regex of the form ^((?:[^_]*_){n-1}[^_]*)_(.*) where n is a variable:

n=2 s='20_231_myString_234' m=re.match(r'^((?:[^_]*_){%d}[^_]*)_(.*)' % (n-1), s) if m: print m.groups() 

or have a nice function:

import re def nthofchar(s, c, n):     regex=r'^((?:[^%c]*%c){%d}[^%c]*)%c(.*)' % (c,c,n-1,c,c)     l = ()     m = re.match(regex, s)     if m: l = m.groups()     return l  s='20_231_myString_234' print nthofchar(s, '_', 2) 

Or without regexes, using iterative find:

def nth_split(s, delim, n):      p, c = -1, 0     while c < n:           p = s.index(delim, p + 1)         c += 1     return s[:p], s[p + 1:]   s1, s2 = nth_split('20_231_myString_234', '_', 2) print s1, ":", s2 
like image 27
perreal Avatar answered Oct 05 '22 22:10

perreal