If you need not use regex, you could use
s="(Canberra)-[:capital_of {}]->(Australia)"
entityA = s[1:].split(')-')[0]
entityB = s.split('->(')[-1][:-1]
The input string is split on the basis of occurrence of the ')-' sub-string and the first part is taken to obtain the first entity.
The split() is done on the basis of the '->(' sub-string and the last split is chosen to obtain the second entity.
So,
print(f'EntityA: {entityA}')
print(f'EntityB: {entityB}')
would give
EntityA: Canberra
EntityB: Australia
Non regex solutions are usually faster.
Edit: Timings as requested in comments.
s="(Canberra)-[:capital_of {}]->(Australia)"
def regex_soln(s):
pattern = r'\((.*)\)\-\[(:.*)\]\-\>\((.*)\)'
rv = re.match(pattern,s).groups()
return rv[0], rv[-1]
def non_regex_soln(s):
return s[1:].split(')-')[0], s.split('->(')[-1][:-1]
%timeit regex_soln(s)
1.47 µs ± 60.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit non_regex_soln(s)
619 ns ± 30.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)