Home > Enterprise >  Split string by multiple delimiters and keep delimiter
Split string by multiple delimiters and keep delimiter

Time:05-14

I know there are multiple SO answers on how to split a string using multiple delimiters and also how to keep a delimiter after splitting a string. But none seem to answer exactly what I'm looking to do. If my input looks like:

How are you? I am fine.
I am also fine. What day is it?

I am only concerned with sentences ending in a . or ?. If it makes it easier, I only need to keep the first sentence, including the punctuation. My results should look like:

How are you?
I am also fine.

Is there a way to do this?

CodePudding user response:

Use a regular expression that matches everything up to the first . or ?.

import re

line = 'How are you? I am fine.'
match = re.match(r'.*?[.?]',line)
if match:
    first_sentence = match.group(0)

CodePudding user response:

Split by row .split(‘\n’) to isolate each sentences

And get the index of first punctuation you see in sentence and split with it .split(‘.’ Or ´?’, 1)

  • Related