The beginning of the regex engine matches all characters up to the end of the line, i.e. greedy .* , then tries to match (test|$) , that is, either the string literal 'test' or the end of the string. Since the first greedy match .* Matches all characters, it is a back-references character and then tries to match again (test|$) , here $ matches the end of the line.
The reason for your match result will be end of line character
I think sed uses POSIX NFA , which tries to find the longest match in Alternation, which is different from R , which seems to use Traditional NFA
Akash source share