0

A fairly simple regex rule I'm trying to create. However, I'm only finding tips for captures of the word and not whole phrases.

I want to capture all lines that contains red unless it also contains ball. It doesn't matter in what order. I just only need to capture red only without ball.

I have a red ball
I have a red cup
I have a green ball
This cup that I have is red

The bonus would be a regex rule for the inverse of this. Capture red and ball not not ball only.

Any help on this would be much appreciated.

Edit:

Unfortunately it's being downvoted with a link to another post that isn't quite the same question. I apologize for the confusion.

Antonio
  • 731
  • 7
  • 28
  • Try this `grep red file | grep -v ball` – llllllllll Mar 20 '18 at 19:33
  • This is some regex for a yaml file. I can't use grep – Antonio Mar 20 '18 at 19:34
  • [mcve] sil vous plait. Show your effort. – user unknown Mar 20 '18 at 19:41
  • I would show my effort but I don't know how to do it unfortunately. It's a hard to google example. A lot of things are talking about negative/positive lookhead but I don't care if it's in front or behind. I can't have it match at all. – Antonio Mar 20 '18 at 19:42
  • 1
    You can use a [negative lookahead](https://www.regular-expressions.info/lookaround.html), e.g. `red(?!.*ball)`. Whether or not it will work your case depends on the language and regex engine that you are using. Please clarify that if you want to receive better answers – Andrea Corbellini Mar 20 '18 at 19:43
  • 1
    `^(?!.*\bball\b).*\bred\b.*` – ctwheels Mar 20 '18 at 19:45
  • @AndreaCorbellini your regex only works if the word `red` precedes the word `ball`. The OP also wants the inverse. – ctwheels Mar 20 '18 at 19:46
  • @ctwheels: sure, and also lacks word boundaries. This is why I posted a comment and not an answer. You can go ahead and post yours as an answer :-) – Andrea Corbellini Mar 20 '18 at 19:48
  • `^(?=.*?\bball\b).*?\bred\b.*$` - Do I get the bonus points? ;-) – robinCTS Mar 21 '18 at 09:56
  • Unfortunately this doesn't quite work either. It doesn't match with any results using pythex. I appreciate the help though! – Antonio Mar 22 '18 at 01:41

0 Answers0