python - How to split string at a specific char(set of chars, actually), but with specified length -


i'm pretty sure there's such question..

here's issue - want split string, using specified chars delimiters, want substrings have length, close specified.


real world example - split long subtitles lines.

example:

1234,asd dsa qwerty 567, 

i want split line number of lines max length, let's 10, don't want "split" words. so, should become:

1234,asd dsa qwerty  567,  

of course, can split lines delimiters , concatenate them again, till reach desired length, terribly slow.

i thought using str.find (and use returned position) can't work regex (because of different delimiters - ., ,, ;, \n, , etc.).

i think re.findall, can't think of regex. thought something like

(.*){, max_len}\s 

with re.s, it's not working. there should tricky way..

the following code splits string desired @ spaces width of 10:

import re r = "1234,asd dsa qwerty 567," p = re.compile("(.{,10})($|\s)") r = p.sub("\\1\n", r) 

in case produces output

1234,asd dsa qwerty 567, 

when split width 5 get

1234,asd dsa qwerty 567, 

you can see, words never split method.

if other delimiters replace "\s" desired regular expression.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -