last day (15 days later) » 

5:59 AM
0
Q: How can I split a text by (a), (b)?

ayansalt2I want to split my text by subparts (a), (b), ... import re s = "(a) First sentence. \n(b) Second sentence. \n(c) Third sentence." l = re.compile('\(([a-f]+)').split(s) With my regex I get a list of 7 elements: ['', 'a', ') First sentence. \n', 'b', ') Second sentence. \n', 'c', ') Third sente...

 
What is your question about this? Is the code you have shown not working? What exactly is the problem?
 
What exact result do you expect?
 
Will there always be a '\n' before (*)?
 
@khelwood what I want is a list with the first element (a) the first sentence, the second item (b) and the third and last item (c)
 
i was thinking more like s.split('\n'). this would section it out.
 
5:59 AM
Your text is in s but you are splitting text.
 
@rohitt yes exactly
 
The result from re.compile('\(([a-f]+)').split(s) is ['', 'a', ') Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n', 'b', ') Nullam porta aliquet ornare. Integer non ullamcorper nibh. Curabitur eu maximus odio. Mauris egestas fermentum ligula non fermentum. Sed tincidunt dolor porta egestas consequat. Nullam pharetra fermentum venenatis. Maecenas at tempor sapien, eu gravida augue. Fusce nec elit sollicitudin est euismod placerat nec ut purus. \n', 'c', ') Phasellus fermentum enim ex. Suspendisse ac augue vitae magna convallis dapibus.'] which looks reasonable.
 
You should try s.split('\n'). this seems to work
 
... Though perhaps not what you want; again, please edit your question to clarify what you actually want and in what ways you would like to change the result.
 
I believe @tripleee could update to include '(a)' and '(b)' and turn it into an answer
 
5:59 AM
@tripleee I did, is it clear ?
 
Your updated question does not contain a minimal reproducible example; the code you have returns 7 elements.
 
@MohammedAadil but sometimes I don't have \n before (a)
 
The capturing parentheses in your regex cause the a and b to be expanded into the results, take out the parentheses if you don't want that. split() accepts a max argument so you can limit to, say, three elements if your real text is much longer and contains additional matches.
re.compile('\s*\([a-f]+\)\s*').split(s, 3) gets me a list of four elements, where the first is empty (because you are splitting on the text on both sides of (a)).
 
@tripleee is it possible to keep (a) before the text ?
 
Of course, but really, it's quite tiresome to repeatedly ask you to edit your question to show us exactly (1) what you have, (2) what you want, and (3) what you have tried, where the code you show actually produces the result you claim you are getting from your example input. If you can fix your question so that it contains all your requirements and a reasonable minimal reproducible example it can be reopened and receive answers. Perhaps also review other sections in the help center, especially How to ask.
 
5:59 AM
Why don't you just s.split('\n')?
 
@accdias Must be for stability.
 
@ayansalt2 please check my edit to your question and see what a minimal reproducible example should look like. There is no need for such long and confusing string, that's the minimal part. Also remember to always be clear about the input and the output you expect
 
@AnnZen, to me it looks like the delimiter is clearly a \n so it is like trying to reinvent the wheel with re. The string class already has methods to deal with that: split() or even better splitlines().
 
@accdias There might be plenty of sentences that contain \n as well.
 

last day (15 days later) »