Home > Blockchain >  Simple grep regex to catch [0 0] but not [00]
Simple grep regex to catch [0 0] but not [00]

Time:08-08

Buongiorno,

I am trying to grep all the patterns that look like: [00 0] or [ 0000] but not [00] or [0000] : there has to be at least one space.

Naively, I thought that

grep -P '\[.* .*\]' 

would do it ; when I copy paste the regex in regex101 with some test cases, I get the following:

unmatched:
0 0
000
[00]
[0]
[00]

matched:
[0 0]
[ 0]
[0 ]
[  0  ]
[ 0 ]

which is exactly what I want.

But grep seems to match also the things between brackets but with no space:

[00]
[0]
[00]

What am I missing ?

\[0* 0*\]

seems to work but I'm going to have cases where it's not only 0...

CodePudding user response:

This grep would work with BRE (basic regex), it doesn't require ERE (extended regex) or experimental -P (perl regex):

grep '\[[^] ]* [^]]*]' file

[0 0]
[ 0]
[0 ]
[  0  ]
[ 0 ]

If you have only zeroes and spaces inside the [...] then try:

grep '\[0* [0 ]*]' file

Online Demo

RegEx Details:

  • \[: Match a [
  • [^] ]*: Match 0 or more of any chars that are not ] and space
  • : Match a space
  • [^]]*: Match 0 or more of any chars that are not ]
  • ]: Match a ]

Problem with .* is that it matches 0 or more of any character except a line break. It won't stop at ].

  • Related