Unscramble those case-(very)-sensitive strings
Retina, 53 bytes
Not really clever, but a clean and quite readable solution
([a-z])(.*?)([a-z])
$3$2$1
([A-Z])(.*?)([A-Z])
$3$2$1
Try it online!
MATL, 22 bytes
2:"tttk<f2etAZ))P5M(Yo
Try it online! Or verify all test cases.
How it works
2:" % Do the following twice
ttt % Input string (implicit). Push three more copies
k % Convert to lowercase
<f % Indices of characters that had their code point increased by
% the lowercase conversion, i.e. that were uppercase letters
2e % Convert to 2-row matrix. This pads a zero in the lower-right
% corner if necessary
tAZ) % Keep only columns that don't contain zeros. Thus if there
% was a character that can't be swapped it will be ignored
) % Get 2-row matrix of characters at those positions
P % Flip vertically. This does the swapping
5M % Push matrix of original indices again
( % Write the swapped characters onto their original positions
Yo % Change case. In the first iteration, this prepares the
% string so the second iteration will process the letters that
% were originally lowercase. In the second iteration, it
% undoes the change of case
% End (implicit)
% Display (implicit)
Jelly, 21 20 19 18 bytes
s2UF,
nŒlTÇyJịŒsµ⁺
Try it online!
How it works
nŒlTÇyJịŒsµ⁺ Main link. Argument: s (string)
Œl Convert to lowercase.
n Test for inequality.
T Truth; yield all indices of 1's.
Ç Call the helper link. Yields [A, B] (pair of lists).
J Indices; yield I := [1, ..., len(s)].
y Translate; replace the integers of I that occur in A with the
corresponding integers in B.
Œs Swapcase; yield s with swapped case.
ị Use the translated index list to index into s with swapped case.
µ Combine all links to the left into a chain.
⁺ Duplicate the chain, executing it twice.
s2UF, Helper link. Argument: J (list of indices)
s2 Split J into pairs. If the length is odd, the last list will be
a singleton list.
U Upend; reverse each pair. This is a no-op for singletons lists.
F Flatten, concatenating the pairs.
, Pair the previous result with J.