-3

I'm very new to regex and I've looked but haven't found the syntax I'm looking for.

I want to match any word (including foobar), but not foo. However, everythimg I've found catches foobar with foo.

What's the correct way to do this? I'm working in Python, if that matters

user1543042
  • 3,422
  • 1
  • 17
  • 31

1 Answers1

2

(?!^foo$)^\w+$

This is a negative look ahead (?!), saying don't match the word foo, but match any other word.

^ and $ assert the start and end of the string, respectively. \w+ means match one or more of any word character.

And an example:

https://regex101.com/r/nfxyso/2

Mako212
  • 6,787
  • 1
  • 18
  • 37
  • I believe the OP is trying to figure how to match whole words and not their parts. – gregory Aug 24 '18 at 18:14
  • This does match whole words and not their parts... – Mako212 Aug 24 '18 at 18:15
  • 1
    @Mako212 Try adding a period at the end of foobar -- No match. And while you could modify it to perhaps deal with punctuation, the regex leads one away from the much simpler and more commonly used idea of a word boundary, which I think is what a beginner should be introduced to first. – gregory Aug 24 '18 at 18:23
  • Fair on `\b`. You can drop the `$` to match `foobar` followed by a period, but given the lack of detail in OPs question, it's out of scope whether that case is matched or not. – Mako212 Aug 24 '18 at 18:27