John John - 4 months ago 20
Bash Question

sed - regex square brackets detection in Linux

I am using Ubuntu 14.04, and I have the following statement:

192.168.2.4 [text to capture] Test: This is a test statement.


I am trying to capture "text to capture" using the following regex:

echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^\[\]]*\[(.*)\].*$/\1/"


The idea behind the regex is to traverse over all characters which do not match opening and closing square bracket. Once an opening square bracket is encountered, capture the text until the closing bracket is encountered, then ignore all subsequent characters.

When I use the regex above in a regex tester, I can see that the "text to capture" is being captured.

However, executing the regex command above returns the complete statement aka:

echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^\[\]]*\[(.*)\].*$/\1/"


Can anyone spot what I have missed here? I believe I have escaped the characters brackets correctly, since it is working correctly with the regex tester.

Thanks
John

Answer

You actually need to exclude only for the starting [ in the first statement:

echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^[]*\[(.*)\].*$/\1/"

If you really want both [ and ] inside a [^ ] just use [^][] and you don't need escaping.

Comments