Python Regular expression must strip whitespace except between quotes

后端 未结 5 1260
一个人的身影
一个人的身影 2020-12-11 18:43

I need a way to remove all whitespace from a string, except when that whitespace is between quotes.

result = re.sub(\'\".*?\"\', \"\", content)
5条回答
  •  臣服心动
    2020-12-11 19:12

    Oli, resurrecting this question because it had a simple regex solution that wasn't mentioned. (Found your question while doing some research for a regex bounty quest.)

    Here's the small regex:

    "[^"]*"|(\s+)
    

    The left side of the alternation matches complete "quoted strings". We will ignore these matches. The right side matches and captures spaces to Group 1, and we know they are the right spaces because they were not matched by the expression on the left.

    Here is working code (and an online demo):

    import re
    subject = 'Remove Spaces Here "But Not Here" Thank You'
    regex = re.compile(r'"[^"]*"|(\s+)')
    def myreplacement(m):
        if m.group(1):
            return ""
        else:
            return m.group(0)
    replaced = regex.sub(myreplacement, subject)
    print(replaced)
    

    Reference

    1. How to match pattern except in situations s1, s2, s3
    2. How to match a pattern unless...

提交回复
热议问题