删除字母数字单词,但Python3中有一些例外

2024-10-01 22:38:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过删除字母数字单词(同时包含字母和数字的单词)来清理python3中的一些文本体,但是有一些例外情况我希望保留在文本中。以下代码从文本中删除所有字母数字单词:

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)

上面的代码生成新的字符串1是“游戏表”,新的字符串2是“土豆”。我需要的是新的字符串1是“游戏xbox360表”和新的字符串2是“ps4土豆”。在

我想我可以创建一系列异常,例如:

^{pr2}$

但是不太确定如何将这个异常列表合并到正则表达式中(我对这个概念还比较陌生)。任何洞察力都是非常感谢的!在


Tags: 字符串文本re游戏new字母数字单词
3条回答

我找不到正则表达式,但这里有一种方法可以实现它

>>> 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']


另外,我将正则表达式改为 ^{pr2}$

并尝试在每个“单词”的开头(用re.match())匹配它们,因为\w也包含数字。在


如果能够升级到newer ^{} module,则可以使用(*SKIP)(*FAIL)和更好的表达式,而不需要函数:
\b(?:xbox360|ps4)\b   # define your exceptions
(*SKIP)(*FAIL)        # these shall fail
|                     # or match words with digits
\b[A-Za-z]*\d\w*\b

请参见a demo on 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']

消极使用。一个负的lookahead是zero length:它与任何内容都不匹配;它要么成功,要么失败,并且在完成后,光标仍然在它之前的位置。因此,您需要检查单词边界(\b),检查以下文本是否不在异常列表((?!...))中,并使用现有的正则表达式匹配单词(\w*\d\w*)。在

要构造lookahead的主体,只需将exceptions的元素与其间的|串联在一起,或者使exceptions成为一个与您希望直接保留的单词相匹配的regex。在

我对Python不太熟悉,下面是示例中regex应该是什么样子的,我希望您可以概括一下:

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

删除空白

^{pr2}$

相关问题 更多 >

    热门问题