Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep regex not working with square brackets

So I was trying to write a regex in grep to match square brackets, i.e [ad] should match [ and ]. But I was getting different results on using capturing groups and character classes. Also the result is different on putting ' in the beginning and end of regex string.

So these are the different result that I am getting.

Using capturing groups works fine

echo "[ad]" | grep -E '(\[|\])'
[ad]

Using capturing groups without ' gives syntax error

echo "[ad]" | grep -E (\[|\])
bash: syntax error near unexpected token `('

using character class with [ followed by ] gives no output

echo "[ad]" | grep -E [\[\]]

Using character class with ] followed by [ works correctly

echo "[ad]" | grep -E [\]\[]
[ad]

Using character class with ] followed by [ and using ' does not work

echo "[ad]" | grep -E '[\]\[]'

It'd be great if someone could explain the difference between them.

like image 263
Prashant Gupta Avatar asked Oct 20 '25 11:10

Prashant Gupta


1 Answers

You should know about:

BRE ( = Basic Regular Expression )

ERE ( = Extended Regular Expression )


BRE metacharacters require a backslash to give them their special meaning and grep is based on

The ERE flavor standardizes a flavor similar to the one used by the UNIX egrep command.


Pay attention to -E and -G

grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  ...
  ...

POSIX Basic Regular Expressions

POSIX Extended Regular Expressions

POSIX Bracket Expressions


And you should also know about bash, since some of your input is related to bash interpreter not grep or anything else

echo "[ad]" | grep -E (\[|\])

Here bash assumes you try to use () something like:

echo $(( 10 * 10 ))

and by using single quote ' you tell the bash that you do not want it treats as a special operator for it. So

echo "[ad]" | grep -E '(\[|\])'

is correct.

like image 157
Shakiba Moshiri Avatar answered Oct 23 '25 02:10

Shakiba Moshiri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!