0

Is there a way to use split function without losing the word or char, that you using to split with?

for example:

import re
x = '''\
1.
abcde.
2.
fgh 2.5 ijk.
3.
lmnop
    '''
print(x)

listByNum = re.split(r'\d\.\D', x)

print(listByNum) 

the output I want to keep the digit in the list

An other example:

import re
x = '''\
I love stackoverflow. I love food.\nblah blah blah.
    '''
print(x)

listBySentences = re.split(r'\.', x)

print(listBySentences)

output for example 2

3
  • (?=\d\.\D)|(?<=\d\.\D) should split on both sides of it, creating a separate element for \d\.\D. If you want it on one side or the other, remove one of the assertions, keep the other. Note that you can't get one side or the other if you exclude the \.D Commented Aug 10, 2016 at 9:44
  • @sln: Note that you cannot split on an empty string in python's re module - you need to use regex instead. Commented Aug 10, 2016 at 11:49
  • @Jan - Note that you cannot split on an empty string Can you give me an example how this relates to my comment? Commented Aug 14, 2016 at 19:45

1 Answer 1

2

Not very well documented, but you can use parentheses around the expression in question:

import re
x = '''\
1.
abcde.
2.
fgh 2.5 ijk.
3.
lmnop
    '''
print(x)

listByNum = re.split(r'(\d\.\D)', x)

print(listByNum) 
# ['', '1.\n', 'abcde.\n', '2.\n', 'fgh 2.5 ijk.\n', '3.\n', 'lmnop\n    ']


To even clean your data afterwards, you can use a list comprehension, like so:

listByNum = [num.strip() for num in re.split(r'(\d\.\D)', x) if num]
# ['1.', 'abcde.', '2.', 'fgh 2.5 ijk.', '3.', 'lmnop']


To keep the digits within the splitted elements, you can use the newer regex module which supports splitting on empty strings:

import regex as re
x = same string as above
listByNum = [num.strip() for num in re.split(r'(?V1)(?=\d\.\D)', x) if num]
# ['1.\nabcde.', '2.\nfgh 2.5 ijk.', '3.\nlmnop']
Sign up to request clarification or add additional context in comments.

1 Comment

the number are in separate element, can I keep them together. # ['1. abcde.', '2. fgh 2.5 ijk.', '3. lmnop']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.