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 9015551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:46:37+00:00 2026-06-16T03:46:37+00:00

I’m trying to create a Sublime Text plugin (using python) that reverses the order

  • 0

I’m trying to create a Sublime Text plugin (using python) that reverses the order of words in a selected string. I’ve got the main functionality working but now my problem is that every symbol at the end of a word (periods, commas, question marks etc) stays where it is, and my goal is to get everything reversed properly so the symbols should move to the start of the word.

def run(self, edit):
    selections = self.view.sel()

    # Loop through multiple text selections
    for location in selections:

        # Grab selection
        sentence = self.view.substr(location)

        # Break the string into an array of words
        words = sentence.split()

        # The greasy fix
        for individual in words:
            if individual.endswith('.'):
                words[words.index(individual)] = "."+individual[:-1]

        # Join the array items together in reverse order
        sentence_rev = " ".join(reversed(words))

        # Replace the current string with the new reversed string
        self.view.replace(edit, location, sentence_rev) 

    # The quick brown fox, jumped over the lazy dog.
    # .dog lazy the over jumped ,fox brown quick The

I’ve been able to loop through each word and use the endswith() method for a quick fix but this will not find multiple symbols (without a long list of if statements) or account for multiple symbols and move them all.

I’ve been playing around with regex but still haven’t got a solution that works, and I’ve been looking for a way to change the index of the symbol but still nothing…

If I can give any more details please let me know.

Thanks!

  • 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-06-16T03:46:38+00:00Added an answer on June 16, 2026 at 3:46 am

    If you import re you can change your split() line to split on the word break \b instead:

    words = re.sub(r'\b', '\f', sentence).split('\f')
    

    See this for why you can’t just split(r'\b'). The above will give you:

    ['', 'The', ' ', 'quick', ' ', 'brown', ' ', 'fox', ', ', 'jumps', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog', '.']
    

    Which you can then easily reverse and get your symbols in the correct place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.