Using regex in Mac OS X: why I have to add \ in {3, 5} ==> \{3, 5\} ...? I am trying to find all files with no-extension in a particular folder, and after many attempts I finally came up with this regex statement:


  ls | grep "\.[A-Za-z]\{3,5\}$"


Why I have to start the occurrences around the {} with \, like escaping the sequence? Is there a way to use standard regex like this?


  ls | grep "\.[A-Za-z]{3,5}$"


I want, ultimately to get the opposite list of files with no-extension, but this non standard regex is making it hard for me to negate the result from the grep.
If I understand your question correctly, you do not need to escape the curly-braces with a backslash if you form the command properly.

In other words, when using a RegRx with grep, let it know the search pattern is a RegEx by using the -E option.

If you want an inverse match then use the -v option as well.

Example:

ls | grep -E -v '\.[A-Za-z]{0,5}$'



Note I used {0,5}$ instead of {3,5}$ to account for any extension up to 5 characters. If you want to include files that might have a 1 or 2 character extensions, then use {3,5}$ instead.


You can also use the following example:

ls | grep -E -v '\.[[:alpha:]]{0,5}$'


Sometimes a file extension can have numbers in them, so to account for that, use as an example:

ls | grep -E -v '\.[A-Za-z0-9]{0,5}$'


Or:

ls | grep -E -v '\.[[:alnum:]]{0,5}$'


Note: Some commands require the options to be passed individually, while others can be combined. In this case, grep does allow options to be concatenated. So in the example commands, -E -v can be expressed as -Ev or -vE, if you wish.