Tiebreaker for same-length regex alternatives with same starting position
问题 Using GNU sed (with the -r flag for clarity), the following two substitutions on the input string ab give the same result: s/(.)(.)|(.)(.)$/\2\1\3\4/ and s/(.)(.)$|(.)(.)/\1\2\4\3/ both give ba . It would appear that the alternative (.)(.) (the one without $ ) succeeds in both substitutions, regardless of whether its position as the first or second alternative. Why is this the case? What is the tie-breaker for such alternatives? The POSIX specification of regular expressions specifies 1 the