python regex inserting a space between punctuation and letters


I assume the best way to do this is with regex but I do not know how to do it. I am trying to parse a string and put a space between letters and punctuation only. I want to keep punctuation marks together. As an example if I have the string


I want to end up with

"yes", "!!!".

If I have the string


I want to end up with

"!!!", "N00bs"

Is this possible? What is the best way to do this? Right now I am parsing each letter and it a silly way of doing it.

Thanks for the help.


something like this:

txt = re.sub( r'([a-zA-Z])([,.!])', r'\1 \2', '!!!this, .is, .a .test!!!' )

you can switch the order for the other direction

re.sub( r'([,.!])([a-zA-Z])', r'\1 \2', txt )

probably you can also make it work in one regex as well


If you just want to add a space maybe use replace?

x = x.replace('!',' ')

You may have to use more replace's to remove spaces between punctuation and punctuation.


I'd use:


It works for both yes!!! and !!!N00bs


The regular expression: (?-imsx:(.+)\b(.+)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \b the boundary between a word char (\w) and something that is not a word char ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- .+ any character except \n (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------


  • Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for getAlbum
  • How to remove special characters in file names?
  • PHP Explode from first number (integer)
  • Parsing tokens with PLY
  • Maple: How to insert a command to force my code to choose random integer values to find my values?
  • Enterprise Architect - Cancel Connectors Bridges
  • Update SQL MS Access 2010
  • Add spaces between words in spaceless string
  • Messed up characters in webpages (especially social media)
  • Best practice for switching iPhone views?
  • How to save dynamically created textboxes and their values
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • pip in virtualenv gets ConnectTimeoutError
  • Hide HTML elements without javascript, only CSS
  • SAXReader not re-ecape characters
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Copy to all folders batch file?
  • Installed module is empty
  • Moving mysql files across servers
  • Breeze - Deleted Items nav properties bug
  • Checking free space on FTP server
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • javaw.exe and eclipse startup problems
  • JSON with duplicate key names losing information when parsed
  • Do create extension work in single-user mode in postgres?
  • Jquery - Jquery Wysiwyg return html as a string
  • Return words with double consecutive letters
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • need help with bizarre java.net.HttpURLConnection behavior
  • Android Google Maps API OnLocationChanged only called once
  • Binding checkboxes to object values in AngularJs
  • UserPrincipal.Current returns apppool on IIS
  • How to load view controller without button in storyboard?