簡體   English   中英

在Python 3中刪除字母數字詞,但有一些例外

[英]Removing Alphanumeric Words, With Some Exceptions in Python 3

我試圖通過刪除字母數字單詞(包含字母和數字的單詞)來清理Python 3中的某些正文,但是我想保留一些例外。 以下代碼從文本中刪除所有字母數字單詞:

import re

string1 = "3n3k game gnma34 xbox360 table"
string2 = "the a22b b3kj3 ps4 2ij2aln potato"

new_string1 = re.sub(r'\w*\d\w*', '', string1)
new_string2 = re.sub(r'\w*\d\w*', '', string2)

上面的代碼產生了“游戲桌”的new_string1和“土豆”的new_string2。 我需要的是new_string1是“游戲xbox360表”,而new_string2是“ ps4馬鈴薯”。

我想我可以創建一個異常數組,例如:

exceptions = ['xbox360', 'ps4'] #there may be many more exceptions than this

但是我不太確定如何將此例外列表合並到我的正則表達式中(我對這個概念還很陌生)。 非常感謝任何見識!

使用負數前瞻。 負前瞻長度為零 :不匹配任何內容; 它要么成功,要么失敗,完成之后,游標仍然位於之前的位置。 因此,您要檢查單詞邊界( \\b ),檢查以下文本是否不在您的例外列表( (?!...) )中,並使用現有的正則表達式來匹配單詞( \\w*\\d\\w* )。

要構造超前的主體,只需將exceptions元素與|串聯在一起| 介於兩者之間,或者只是將與您要直接保留的單詞匹配的正則表達式設置為exceptions

我對Python不太熟悉,因此示例示例中的正則表達式應如下所示,希望您能對此進行概括:

\b(?!xbox360|ps4)\w*\d\w*

刪除空格

" ".join(re.sub(r'\b(?!xbox360|ps4)\w*\d\w*'," ",string1).split())

我找不到適合您的正則表達式,但這是實現它的一種方法

>>> exceptions = ['xbox360', 'ps4']
>>> string1 = "3n3k game gnma34 xbox360 table"

>>> " ".join([i if i in exceptions else re.sub(r'\w*\d\w*', '', i) for i in string1.split()])
' game  xbox360 table'
>>> string2 = "the a22b b3kj3 ps4 2ij2aln potato"

>>> " ".join([i if i in exceptions else re.sub(r'\w*\d\w*', '', i) for i in string2.split()])
'the   ps4  potato'

使用兩種方式:拆分並分析單詞:

import re

strings = ["3n3k game gnma34 xbox360 table", "the a22b b3kj3 ps4 2ij2aln potato"]
exceptions = ['xbox360', 'ps4']

def cleanse(word):
    rx = re.compile(r'\D*\d')
    if rx.match(word) and word not in exceptions:
        return ''
    return word

nstrings = [" ".join(filter(None, (
    cleanse(word) for word in string.split()))) 
    for string in strings]
print(nstrings)
# ['game xbox360 table', 'the ps4 potato']


此外,我將正則表達式更改為

 `\\D*\\d` 

並嘗試在每個“單詞”的開頭(使用re.match() )進行匹配,因為\\w包含數字。


如果您能夠升級到較新的regex模塊regex可以使用(*SKIP)(*FAIL)和更好的表達式,而無需使用函數:

import regex as re

strings = ["3n3k game gnma34 xbox360 table", "the a22b b3kj3 ps4 2ij2aln potato  123123 1234"]
exceptions = [r'\d+', 'xbox360', 'ps4']

rx = re.compile(r'\b(?:{})\b(*SKIP)(*FAIL)|\b[A-Za-z]*\d\w*\b'.format("|".join(exceptions)))

nstrings = [" ".join(
    filter(None, (rx.sub('', word) 
    for word in string.split()))) 
    for string in strings]
print(nstrings)
# ['game xbox360 table', 'the ps4 potato 123123 1234']

在regex101.com上查看演示,並在此處查看完整的Python代碼段:

 import regex as re strings = ["3n3k game gnma34 xbox360 table", "the a22b b3kj3 ps4 2ij2aln potato 123123 1234"] exceptions = [r'\\d+', 'xbox360', 'ps4'] rx = re.compile(r'\\b(?:{})\\b(*SKIP)(*FAIL)|\\b[A-Za-z]*\\d\\w*\\b'.format("|".join(exceptions))) nstrings = [" ".join( filter(None, (rx.sub('', word) for word in string.split()))) for string in strings] print(nstrings) # ['game xbox360 table', 'the ps4 potato 123123 1234'] 

暫無
暫無

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

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