Apparently, the regex has to match the corresponding line in both files for it to work. Otherwise it still shows it as a difference.
Code:
$ cat fileA.txt
[Fry]
[Leela]
[Bender]
[Farnsworth]
[Amy]
[Hermes]
[Zoidberg]
[Nibbler]
$ cat fileB.txt
[Fry]
[Leela]
[Bender]
[Prof. Farnsworth]
[Amy]
[Hermes]
[Dr. Zoidberg]
[Nibbler]
$ diff fileA.txt fileB.txt
4c4
< [Farnsworth]
---
> [Prof. Farnsworth]
7c7
< [Zoidberg]
---
> [Dr. Zoidberg]
$ diff -I '^\[Dr.*\]' fileA.txt fileB.txt
4c4
< [Farnsworth]
---
> [Prof. Farnsworth]
7c7
< [Zoidberg]
---
> [Dr. Zoidberg]
$ diff -I '^\[.*Zoid.*\]' fileA.txt fileB.txt
4c4
< [Farnsworth]
---
> [Prof. Farnsworth]
$ diff -I 'Zoid.*\]$' fileA.txt fileB.txt
4c4
< [Farnsworth]
---
> [Prof. Farnsworth]
Inserting a blank line somewhere so that the line numbers don't match up also foils the regex match.
As for escaping characters, first realize that protecting them from the shell and from the regex pattern are two separate things. To fully protect from the shell, you usually should use single-quotes. See here for a full discussion on shell quoting:
http://www.tldp.org/LDP/abs/html/quoting.html
After you've protected the string from the shell and passed it to the program, then you have to escape the regex-reserved characters, including brackets. There are a couple of ways to do this. Backslashing is one way, as I demonstrated above. You can also use brackets to list a range of acceptable characters, including restricted ones such as brackets.
Code:
diff -I '[[].*Zoid.*[]]$' fileA.txt fileB.txt
But to match a ']', it has to be the first character in the list. This doesn't work:
Code:
$ diff -I '[[].*Zoid.*[ ]]$' fileA.txt fileB.txt
More on regex syntax here:
http://www.tldp.org/LDP/abs/html/x16775.html
and here:
http://www.regular-expressions.info/posixbrackets.html