正则表达式匹配python列表中的所有单词都不会被@

0 python regex

我目前正在使用此代码搜索列表中的所有单词,但我需要它忽略前面带@的单词.

[@apples, peaches, oranges, @guava]

只返回:

[peaches, oranges]

words = re.compile(r'\w+')

任何人都可以帮我做同样的事吗?

Zim*_*m3r 6

为什么正则表达式似乎是一个相当简单的任务,没有它和正则表达式似乎更麻烦然后需要,为什么不这...

f = []
for w in l:
    if not w.startswith("@"):
       f.append(w)
Run Code Online (Sandbox Code Playgroud)


Hyp*_*eus 6

试试这个:

[x for x in l if not x.startswith('@') ]
Run Code Online (Sandbox Code Playgroud)

l你未经过滤的原始清单.

或者,如果您想要生成器而不是实际列表,请用括号替换方括号.