Python line break using regex

I need to split a string into an array at word boundaries (spaces), keeping spaces.

For instance:

'this is a\nsentence' 

Would become

 ['this', ' ', 'is', ' ', 'a' '\n', 'sentence'] 

I know about str.partition and re.split , but none of them do what I want and there is no re.partition .

How should separating strings into spaces in Python with reasonable efficiency?

+6
source share
3 answers

Try the following:

 s = "this is a\nsentence" re.split(r'(\W+)', s) # Notice parentheses and a plus sign. 

Result:

 ['this', ' ', 'is', ' ', 'a', '\n', 'sentence'] 
+11
source

Space character in re '\ s' not '\ W'

For comparison:

 import re s = "With a sign # written @ the beginning , that a\nsentence,"\ '\nno more an instruction!,\tyou know ?? "Cases" & and surprises:'\ "that will 'lways unknown **before**, in 81% of time$" a = re.split('(\W+)', s) print a print len(a) print b = re.split('(\s+)', s) print b print len(b) 

produces

 ['With', ' ', 'a', ' ', 'sign', ' # ', 'written', ' @ ', 'the', ' ', 'beginning', ' , ', 'that', "'", 's', ' ', 'a', '\n', 'sentence', ',\n', 'no', ' ', 'more', ' ', 'an', ' ', 'instruction', '!,\t', 'you', ' ', 'know', ' ?? "', 'Cases', '" & ', 'and', ' ', 'surprises', ':', 'that', ' ', 'will', " '", 'lways', ' ', 'unknown', ' **', 'before', '**, ', 'in', ' ', '81', '% ', 'of', ' ', 'time', '$', ''] 57 ['With', ' ', 'a', ' ', 'sign', ' ', '#', ' ', 'written', ' ', '@', ' ', 'the', ' ', 'beginning', ' ', ',', ' ', "that's", ' ', 'a', '\n', 'sentence,', '\n', 'no', ' ', 'more', ' ', 'an', ' ', 'instruction!,', '\t', 'you', ' ', 'know', ' ', '??', ' ', '"Cases"', ' ', '&', ' ', 'and', ' ', 'surprises:that', ' ', 'will', ' ', "'lways", ' ', 'unknown', ' ', '**before**,', ' ', 'in', ' ', '81%', ' ', 'of', ' ', 'time$'] 61 
+4
source

Try the following:

 re.split('(\W+)','this is a\nsentence') 
+3
source

Source: https://habr.com/ru/post/887587/


All Articles