How to make `grep -Po` `.+` not greedy?

```bash
grep -Po '.+(?=\t+zip$)' file
```
May 28, 4:24pm

To make the .+ in your grep -Po command non-greedy (i.e., match as little as possible), you need to add a ? after the + quantifier.

Solution

Change your pattern to:

bash
grep -Po '.+?(?=\t+zip$)' file
  • .+? means: match one or more of any character, as few times as possible (non-greedy).

Explanation

  • .+ is greedy by default, so it will match as much as possible.
  • .+? is non-greedy, so it will match as little as possible before the lookahead (?=\t+zip$) is satisfied.

This ensures that the match stops at the first occurrence of \t+zip$ rather than the last.

May 28, 4:24pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more