简体   繁体   中英

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

"yes!!!"

I want to end up with

"yes", "!!!".

If I have the string

!!!N00bs,

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:

(.+)\b(.+)

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

Explanation:

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
----------------------------------------------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM