折叠字符串中的空格 [英] collapsing whitespace in a string

查看:39
本文介绍了折叠字符串中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的字符串:

I have a string that kind of looks like this:

"stuff   .  // : /// more-stuff .. .. ...$%$% stuff -> DD"

我想去掉所有标点符号,使所有内容大写并折叠所有空格,使其看起来像这样:

and I want to strip off all punctuation, make everything uppercase and collapse all whitespace so that it looks like this:

"STUFF MORE STUFF STUFF DD"

这可以用一个正则表达式还是我需要组合两个以上?这是我目前所拥有的:

Is this possible with one regex or do I need to combine more than two? This is what I have so far:

def normalize(string):
    import re

    string = string.upper()

    rex   = re.compile(r'\W')
    rex_s = re.compile(r'\s{2,}')

    result = rex.sub(' ', string) # this produces a string with tons of whitespace padding
    result = rex.sub('', result) # this reduces all those spaces

    return result

唯一不起作用的是空白折叠.有什么想法吗?

The only thing that doesn't work is the whitespace collapsing. Any ideas?

推荐答案

这是一个单步方法(但大写实际上使用字符串方法——更简单!):

Here's a single-step approach (but the uppercasing actually uses a string method -- much simpler!):

rex = re.compile(r'\W+')
result = rex.sub(' ', strarg).upper()

其中 strarg 是字符串参数(不要使用会影响内置模块或标准库模块的名称,).

where strarg is the string argument (don't use names that shadow builtins or standard library modules, please).

这篇关于折叠字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆