I'm getting this error

RE error: repetition-operator operand invalid

On FreeBSD sed, using csh.

Command is:

sed -rn 's/.*?VIEW ([^\s]+?) (AS .*?)\s([^\s]+?)\s([^\s]+?)/DROP VIEW \1;\nCREATE VIEW \1 \2;/p'

I've seen this question but I don't have any *+s, but I do have some *?s, but that's supposed to mean non-greedy. What am I doing wrong?

  • 2
    "I do have some *?s, but that's supposed to mean non-greedy." In perl, yes, but not in sed.
    – John1024
    Commented May 17, 2016 at 18:30
  • @John1024 No flavours of sed support that? I lifted that regex from somewhere else, weird that someone would post that without trying it at least once.
    – mpen
    Commented May 17, 2016 at 18:56
  • No variant of sed that I know about does that. Can you provide a link to your source?
    – John1024
    Commented May 17, 2016 at 19:47
  • 1
    "super sed" aka ssed supports perl regular expressions with the -R or --regexp-perl options. ssed is forked from GNU sed.
    – cas
    Commented May 18, 2016 at 3:34
  • 1
    OK. That blog post appears to be in error. As you can read here, standard sed does not do non-greedy matches. That includes the GNU, BSD, and POSIX variants. One solution might be to use @cas's suggestion of ssed. Alternatively, you might open a question documenting your input and desired output for that sed command and I bet that there will be good solutions using standard sed and its usual greedy regexes.
    – John1024
    Commented May 18, 2016 at 7:19

1 Answer 1


It was already mentioned in the comments to the question, but to make the answer more explicit:

Non-greedy matching is not available in sed, using either standard or extended regex mode.

If you know the character that should terminate the match, you can use the following technique, as found here: https://0x2a.at/blog/2008/07/sed--non-greedy-matching/

Greedy matching:

$ echo "<b>foo</b>bar" | sed 's/<.*>//g'

Non-greedy matching:

$ echo "<b>foo</b>bar" | sed 's/<[^>]*>//g'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .