如何在Python中将所有特殊字符移动到字符串的末尾?

use*_*058 0 python regex

我正在尝试将所有非字母数字字符过滤到字符串的末尾.我正在使用正则表达式很难,因为我不知道我们的特殊字符在哪里.这里有几个简单的例子.

hello*there*this*is*a*str*ing*with*asterisks
and&this&is&a&str&ing&&with&ampersands&in&i&t
one%mo%refor%good%mea%sure%I%think%you%get%it
Run Code Online (Sandbox Code Playgroud)

我如何将所有特殊字符滑动到字符串的末尾?

这是我尝试过的,但我没有得到任何东西.

re.compile(r'(.+?)(\**)')
r.sub(r'\1\2', string)
Run Code Online (Sandbox Code Playgroud)

编辑:

第一个字符串的预期输出将是:

hellotherethisisastringwithasterisks********
Run Code Online (Sandbox Code Playgroud)

Tig*_*kT3 6

这里不需要正则表达式.只需使用str.isalpha并构建两个列表,然后加入它们:

strings = ['hello*there*this*is*a*str*ing*with*asterisks',
'and&this&is&a&str&ing&&with&ampersands&in&i&t',
'one%mo%refor%good%mea%sure%I%think%you%get%it']
for s in strings:
    a = []
    b = []
    for c in s:
        if c.isalpha():
            a.append(c)
        else:
            b.append(c)
    print(''.join(a+b))
Run Code Online (Sandbox Code Playgroud)

结果:

hellotherethisisastringwithasterisks********
andthisisastringwithampersandsinit&&&&&&&&&&&
onemoreforgoodmeasureIthinkyougetit%%%%%%%%%%
Run Code Online (Sandbox Code Playgroud)

print()Python 3.5及更高版本的替代调用:

print(*a, *b, sep='')
Run Code Online (Sandbox Code Playgroud)