Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4550382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:29:20+00:00 2026-05-21T16:29:20+00:00

How can a string be split on a separator expression while leaving that separator

  • 0

How can a string be split on a separator expression while leaving that separator on the preceding string?

>>> text = "This is an example. Is it made up of more than once sentence? Yes, it is."
>>> re.split("[\.\?!] ", text)
['This is an example', 'Is it made up of more than one sentence', 'Yes, it is.']

I would like the result to be.

['This is an example.', 'Is it made up of more than one sentence?', 'Yes, it is.']

So far I have only tried a lookahead assertion but this fails to split at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:29:21+00:00Added an answer on May 21, 2026 at 4:29 pm
    import re
    
    text = "This is an example.A particular case.Made up of more "\
           "than once sentence?Yes, it is.But no blank !!!That's"\
           " a problem ????Yes.I think so! :)"
    
    
    for x in re.split("(?<=[\.\?!]) ", text):
        print repr(x)
    
    print '\n'
    
    for x in re.findall("[^.?!]*[.?!]|[^.?!]+(?=\Z)",text):
        print repr(x)
    

    result

    "This is an example.A particular case.Made up of more than once sentence?Yes, it is.But no blank !!!That'sa problem ????Yes.I think so!"
    ':)'
    
    
    'This is an example.'
    'A particular case.'
    'Made up of more than once sentence?'
    'Yes, it is.'
    'But no blank !'
    '!'
    '!'
    "That's a problem ?"
    '?'
    '?'
    '?'
    'Yes.'
    'I think so!'
    ' :)'
    

    .

    EDIT

    Also

    import re
    
    text = "! This is an example.A particular case.Made up of more "\
           "than once sentence?Yes, it is.But no blank !!!That's"\
           " a problem ????Yes.I think so! :)"
    
    res = re.split('([.?!])',text)
    
    print [ ''.join(res[i:i+2]) for i in xrange(0,len(res),2) ]
    

    gives

    ['!', ' This is an example.', 'A particular case.', 'Made up of more than once sentence?', 'Yes, it is.', 'But no blank !', '!', '!', "That's a problem ?", '?', '?', '?', 'Yes.', 'I think so!', ' :)']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this question that asks given a string smith;rodgers;McCalne how can you produce
I am using the String.Split() method in C#. How can I put the resulting
Using SQL Server, how do I split a string so I can access item
What is the longest string that can be created in .NET? The docs for
I know there are libs in other languages that can take a string that
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
I normally use the following idiom to check if a String can be converted
I am writing a fairly simple Django application where users can enter string queries.
In .Net you can read a string value into another data type using either
In javascript, we can do: [a string, 10, {x : 1}, function() {}].push(another value);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.