Joshro
Joshro

Reputation: 59

Match a string that has lowercase char followed by the same character in uppercase

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

Answers (2)

rowboat
rowboat

Reputation: 428

GNU sed:

sed -E 'h;s/[A-Z]+/\n&/g;/(.)\n\1/I!d;x' file

hold 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

Sundeep
Sundeep

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-insensitive

Upvotes: 2

Related Questions