Skip to content

Instantly share code, notes, and snippets.

@okineadev
Last active May 9, 2024 08:33
Show Gist options
  • Save okineadev/d851f432cd1a462412c212c6e0fc9d04 to your computer and use it in GitHub Desktop.
Save okineadev/d851f432cd1a462412c212c6e0fc9d04 to your computer and use it in GitHub Desktop.
Solving "grep: empty (sub)expression" error on macOS

Solving "grep: empty (sub)expression" error on macOS

I ran into this problem when using grep on macOS and found a solution

First of all, I advise you to read this article, which describes the solution to the problem - https://qiita.com/t_o_d/items/fb1f2dd49723b735df2c


Problem solving:

For example, if you use the following expression:

(ab|cd|)

Then you will get an error "grep: empty (sub)expression"

To prevent this, you need to put $ after cd|:

(ab|cd|$)

Also if if you have the same condition at the beginning of the line, for example:

(ab|cd|)some text(ef|gh|)

Then in the first condition after cd| you need to put ^, and in the second, as in the previous example - $:

(ab|cd|^)some text(ef|gh|$)

Then everything will work without problems on both macOS and Linux

If you want to test your program on macOS, you can use this guide - https://gist.github.com/okineadev/b80d7f9a065543655e203471d582f3f1

I also advise you to use the site https://regex101.com, it is very easy to create regular expressions there


I hope you found this note useful. Have a good day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment