簡體   English   中英

使用帶字符串的 re.split 時如何放棄特殊字符和數字?

[英]how to abandon special characters and numbers when using re.split with string?

如果我想分割一個保留空格的字符串,但不想包含特殊字符和數字。

所以它看起來像這樣。

sentence = "jak3 love$ $b0x1n%"
list_after_split = ["jak", " ", "love", " ", "bxn"]

我想使用re.split() ,但我不確定要寫什么作為模式。

嘗試先過濾掉不需要的字符:

>>> import re
>>> sentence = "jak3 love$ $b0x1n%"
>>> sentence_filtered = re.sub(r'[^a-zA-Z\s]+', '', sentence)
>>> # Alternative: sentence_filtered = ''.join(ch for ch in sentence if ch.isalpha() or ch.isspace())
>>> sentence_filtered
'jak love bxn'
>>> re.split('(\s+)', sentence_filtered)
['jak', ' ', 'love', ' ', 'bxn']

如果要將空格壓縮為一個空格:

import re

# String with multi-spaces, tab(s), and newline(s).
s='Jak3     \t love$s \n  $D0ax1t3e90r%.'
print(s)
# Jak3         love$s 
#   $D0ax1t3e90r%.

# First, remove all characters which aren't letters or a space.
# Second, condense spaces together into a single space.
# Third, split into desired list.
print(re.split(r'( )', re.sub(r' +',' ',re.sub(r'[^a-zA-Z ]+', '', s))))
# ['Jak', ' ', 'loves', ' ', 'Daxter']

暫無
暫無

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

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