Is there any way to make text transparent when it is in a shape and make non-transparent when it is outside the shape?
It's harder to do than it should be, I guess because AE is layer-based rather than node-based like Fusion etc.

You're going to have to make a copy of both of your layers, parent each copy to the original layer – so you only have to move one of each – and arrange them so that your layer stack looks like

text
solid
solid
text


(or vice versa). Now make the bottom solid a track matte for the text layer, using alpha-inverted, so that the text is only visible where the solid isn't, and make the top text the track matte for the top solid, once again using alpha -inverted, so that it punches a hole in the top solid layer. The top solid will form the left hand part of the design and the bottom text the right side.

You could also use just the original layers and have one of them use difference mode, pre-comp them both and then use a extract effect to turn the black parts into transparency, but you'll then have to do the animating in a separate comp, which can make it harder to work with.

There is also the Set Matte effect. A lot of people struggle with this effect, it's a lot less intuitive than the slightly more complicated solutions above, so you'll have to do some research. A good explainer video here.