Grep正则表达式不使用方括号

时间:2022-12-01 21:43:03

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.

所以我试图在grep中写一个正则表达式来匹配方括号,即[ad]应匹配[和]。但是我在使用捕获组和角色类时获得了不同的结果。将'放在正则表达式字符串的开头和结尾处的结果也不同。

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.

如果有人可以解释它们之间的区别,那就太棒了。

3 个解决方案

#1


2  

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

BRE元字符需要反斜杠才能赋予它们特殊含义,而grep则基于

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

ERE风格标准化类似于UNIX egrep命令使用的风格。


Pay attention to -E and -G

注意-E和-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 , since some of your input is related to bash interpreter not grep or anything else

你也应该知道bash,因为你的一些输入与bash解释器有关,而不是grep或其他任何东西

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

Here assumes you try to use () something like:

这里bash假设您尝试使用()类似于:

echo $(( 10 * 10 ))

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

并且通过使用单引号'你告诉bash你不希望它作为它的特殊操作符。所以

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

is correct.

#2


0  

Firstly, always quote Regex pattern to prevent shell interpretation beforehand:

首先,始终引用正则表达式模式以防止shell解释:

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

Secondly, within [] surrounded by quotes, you don't need to escape the [] inside, just write them as is within the outer []:

其次,在引号包围的[]中,你不需要在[]内部转义,只需将它们写在外部[]中:

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

#3


0  

Maybe you provided such a simple example on purpose (after all, it is minimal), but in case all you really want is to check for existence of square brackets (a fixed string, not regex pattern), you can use grep with -F/--fixed-strings and multiple -e options:

也许你故意提供了这样一个简单的例子(毕竟,它是最小的),但是如果你真正想要的是检查是否存在方括号(固定字符串,而不是正则表达式),你可以使用grep和-F / - 固定字符串和多个-e选项:

$ echo "[ad]" | grep -F -e '[' -e ']'
[ad]

Or, a little bit shorter with fgrep:

或者,使用fgrep稍微缩短一点:

$ echo "[ad]" | fgrep -e '[' -e ']'
[ad]

Or, even:

$ echo "[ad]" | fgrep -e[ -e]
[ad]

#1


2  

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

BRE元字符需要反斜杠才能赋予它们特殊含义,而grep则基于

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

ERE风格标准化类似于UNIX egrep命令使用的风格。


Pay attention to -E and -G

注意-E和-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 , since some of your input is related to bash interpreter not grep or anything else

你也应该知道bash,因为你的一些输入与bash解释器有关,而不是grep或其他任何东西

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

Here assumes you try to use () something like:

这里bash假设您尝试使用()类似于:

echo $(( 10 * 10 ))

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

并且通过使用单引号'你告诉bash你不希望它作为它的特殊操作符。所以

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

is correct.

#2


0  

Firstly, always quote Regex pattern to prevent shell interpretation beforehand:

首先,始终引用正则表达式模式以防止shell解释:

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

Secondly, within [] surrounded by quotes, you don't need to escape the [] inside, just write them as is within the outer []:

其次,在引号包围的[]中,你不需要在[]内部转义,只需将它们写在外部[]中:

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

#3


0  

Maybe you provided such a simple example on purpose (after all, it is minimal), but in case all you really want is to check for existence of square brackets (a fixed string, not regex pattern), you can use grep with -F/--fixed-strings and multiple -e options:

也许你故意提供了这样一个简单的例子(毕竟,它是最小的),但是如果你真正想要的是检查是否存在方括号(固定字符串,而不是正则表达式),你可以使用grep和-F / - 固定字符串和多个-e选项:

$ echo "[ad]" | grep -F -e '[' -e ']'
[ad]

Or, a little bit shorter with fgrep:

或者,使用fgrep稍微缩短一点:

$ echo "[ad]" | fgrep -e '[' -e ']'
[ad]

Or, even:

$ echo "[ad]" | fgrep -e[ -e]
[ad]