Nathan
Nathan

Reputation: 2980

How do I find only whole words using re.search?

I have a list of words built from different HTML pages. Instead of writing rule after rule to strip out different elements, I am trying to go through the list and say if it's not a full word with only alpha characters, just move on. This is not working.

for w in words:
     if re.search('\b[a-zA-Z]\b', w) == None:
          continue

I am horrible with regular expressions (if you can't already tell!), so I could use some help. How would I write it so it checks each w to make sure it only have a-zA-Z in it?

Upvotes: 2

Views: 1406

Answers (1)

Wolph
Wolph

Reputation: 80061

You're almost there. You just have to tell your search to match an entire string of 1 or more characters.

for w in words:
     if re.search('^[a-zA-Z]+$', w) == None:
          continue

Another solution (for this specific case atleast) would be to use isalpha();

for w in words:
    if not w.isalpha():
          continue

Upvotes: 3

Related Questions