Devin Bean
Devin Bean

Reputation: 11

regex match all words in a python list not preeced by @

I am currently using this code to search a list for all words but I need it to disregard those words preceded by @.

[@apples, peaches, oranges, @guava]

return just:

[peaches, oranges]

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

Could anyone help me in to do the same?

Upvotes: 0

Views: 239

Answers (3)

Zimm3r
Zimm3r

Reputation: 3425

Why regex it seems a rather simple task without it and regex seems more burdensome then needed, why not this...

f = []
for w in l:
    if not w.startswith("@"):
       f.append(w)

Upvotes: 6

Hyperboreus
Hyperboreus

Reputation: 32429

Try this:

[x for x in l if not x.startswith('@') ]

being l your original, unfiltered list.

Or if you want a generator instead of an actual list, replace the square brackets with parentheses.

Upvotes: 6

p.s.w.g
p.s.w.g

Reputation: 149020

You could simply use a negative look-behind. A pattern like this should work:

(?<!@)\b\w+\b

Note the \b (word boundaries) are there to ensure that it matches the whole word—without them it would match pples and uava.

Upvotes: 1

Related Questions