簡體   English   中英

如何忽略[az] [AZ]以外的字符

[英]how to Ignore characters other than [a-z][A-Z]

如何在python輸入字符串中忽略[az] [AZ]以外的字符,並且在應用方法后,字符串將是什么樣?

我需要使用正則表達式嗎?

如果需要使用正則表達式,請使用負字符類( [^...] ):

re.sub(r'[^a-zA-Z]', '', inputtext)

否定字符類會匹配類中命名的任何字符。

演示:

>>> import re
>>> inputtext = 'The quick brown fox!'
>>> re.sub(r'[^a-zA-Z]', '', inputtext)
'Thequickbrownfox'

但使用str.translate()遠遠快:

import string
ascii_letters = set(map(ord, string.ascii_letters))
non_letters = ''.join(chr(i) for i in range(256) if i not in ascii_letters)
inputtext.translate(None, non_letters)

使用str.translate()比正則表達式快十倍以上:

>>> import timeit, partial, re
>>> ascii_only = partial(re.compile(r'[^a-zA-Z]').sub, '')
>>> timeit.timeit('f(t)', 'from __main__ import ascii_only as f, inputtext as t')
7.903045892715454
>>> timeit.timeit('t.translate(None, m)', 'from __main__ import inputtext as t, non_letters as m')
0.5990171432495117

使用Jakub的方法仍然較慢:

>>> timeit.timeit("''.join(c for c in t if c not in l)", 'from __main__ import inputtext as t; import string; l = set(string.letters)')
9.960685968399048

您可以使用regex

re.compile(r'[^a-zA-Z]').sub('', your_string)

您也可以不使用正則表達式進行管理(例如,如果您遇到了正則表達式恐懼症):

import string
new_string = ''.join(c for c in old_string
                     if c not in set(string.letters))

盡管我將使用正則表達式,但此示例還具有其他教育意義: setcomprehension字符串庫。 請注意,這里並不需要嚴格set

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM