删除列表中字符串的最后一个特殊字符

时间:2019-03-15 20:18:52

标签: python string replace

我有一个单词数组,有些单词以特殊字符结尾。我希望删除单词末尾的所有特殊字符。有一种优雅的方法吗?

aArray=["palabra...","algo,.", "si ...", "onomatopeña", "asi;","www.google.com"]

输出:

aArray=["palabra","algo", "si", "onomatopeña", "asi","www.google.com"]

我正在尝试:

rxx = re.compile(r'(.*)([.,]{2,})')  # Extend [.,] as needed; {2,} means >= 2
aArray=["encontarla....", "esta,.", "sr.", "texto", 'www.google.com', 'encontrarla.']
aArray=([rxx.sub(lambda m: m.group(1), word) for word in a])

我想我根本听不懂。例如,字符串www.google.com是url,不应消除点。

3 个答案:

答案 0 :(得分:2)

您可以使用正则表达式来执行此操作。尽管您对“特殊字符”的定义不是很清楚,但是下面的示例代码给出了您发布的输出:

DECLARE @schemaName sysname = 'myfunschema';
-- shortest
If EXISTS (SELECT 1 WHERE SCHEMA_ID(@schemaName) IS NOT NULL)
PRINT 'YEA'
ELSE
PRINT 'NOPE'

SELECT DB_NAME() AS dbname WHERE SCHEMA_ID(@schemaName) IS NOT NULL -- nothing returned if not there

IF NOT EXISTS ( SELECT  top 1 *
                FROM    sys.schemas
                WHERE   name = @schemaName )
PRINT 'WOOPS MISSING'
ELSE
PRINT 'Has Schema'

SELECT SCHEMA_NAME(SCHEMA_ID(@schemaName)) AS SchemaName1 -- null if not there otherwise schema name returned

SELECT SCHEMA_ID(@schemaName) AS SchemaID1-- null if not there otherwise schema id returned


IF EXISTS (
    SELECT sd.SchemaExists 
    FROM (
        SELECT 
            CASE 
                WHEN SCHEMA_ID(@schemaName) IS NULL THEN 0
                WHEN SCHEMA_ID(@schemaName) IS NOT NULL THEN 1
                ELSE 0 
            END AS SchemaExists
    ) AS sd
    WHERE sd.SchemaExists = 1
)
BEGIN
    SELECT 'Got it';
END
ELSE
BEGIN
    SELECT 'Schema Missing';
END

输出:

import re

aArray=["palabra...","algo,.", "si ...", "onomatopeña", "asi;", "www.google.com"]

for i in range(len(aArray)):
    aArray[i] = re.sub(r'[.,;]+$', '', aArray[i]).strip()

如果“特殊字符”是指任何非字母数字,那么您可以使用此:

['palabra', 'algo', 'si', 'onomatopeña', 'asi', 'www.google.com']

输出:

import re

aArray=["palabra...","algo,.", "si ...", "onomatopeña", "asi;", "www.google.com"]

for i in range(len(aArray)):
    aArray[i] = re.sub(r'[^\w]+$', '', aArray[i]).strip()

还要注意['palabra', 'algo', 'si', 'onomatopeña', 'asi', 'www.google.com'] ,它是在其中删除尾随空格

更新

strip()位于正则表达式的末尾,表示我们希望此模式位于末尾,并且之后不应再包含其他任何内容。这样它也可以处理您的URL。

答案 1 :(得分:2)

仅从字符串末尾去除所有非单词字符:

import re

aArray = ["palabra...", "algo,.", "si ...", "onomatopeña", "asi;", "www.google.com"]

aArray = [re.sub(r'\W+$', '', s) for s in aArray]

结果:

['palabra', 'algo', 'si', 'onomatopeña', 'asi', 'www.google.com']

说明:

\W+匹配任意数量的非单词字符,并且$将匹配项锚定到字符串的末尾。

答案 2 :(得分:1)

这可以使用列表理解和filter来完成,而无需使用正则表达式:

>>> aArray=["palabra...","algo,.", "si ...", "onomatopeña", "asi;","www.google.com"]
>>> [s.rstrip('.;, ') for s in aArray]
['palabra', 'algo', 'si', 'onomatopeña', 'asi', 'www.google.com']

请注意,我假设'.;, '是您所指的所有“特殊字符”。