Reputation: 59
Strings that should match:
foObar
foobBar
Should not match:
fOobar
foobbar
I tried the following without success:
sed -nE '/([a-z])\U\1/p' <<<foObar
Upvotes: 0
Views: 195
Reputation: 428
GNU sed
:
sed -E 'h;s/[A-Z]+/\n&/g;/(.)\n\1/I!d;x' file
h
old the original pattern space. Insert a newline between potential matches. If the is no case-insensitive match of <char><newline><same char>
, then delete. Otherwise swap back to the original pattern space before printing.
For either case followed by same letter of opposite case:
sed -E 'h;s/[A-Z]+|[a-z]+/\n&/g;/(.)\n\1/I!d;x' file
Upvotes: 0
Reputation: 23667
With GNU grep
and PCRE option:
$ cat ip.txt
foObar
foobBar
fOobar
foobbar
# lowercase followed by uppercase version
$ grep -P '([a-z])(?!\1)(?i:\1)' ip.txt
foObar
foobBar
# either case followed by the opposite case
$ grep -P '([a-zA-Z])(?!\1)(?i:\1)' ip.txt
foObar
foobBar
fOobar
(?!\1)
is a negative lookahead that helps to avoid same case as the next character(?i:\1)
makes the backreference case-insensitiveUpvotes: 2