import re
def testReplaceBetweenWords( ) :
head_dlmt= 'Head'
tail_dlmt= 'Tail'
line0 = "abc_Head_def_Head_inner_inside_Tail_ghi_Tail_jkl"
line1 = "abc_Head_first_Tail_ghi_Head_second_Tail_opq"
between_pattern = "^((?:(?!{1}).)*{0}).*?({1}.*)$" .format ( head_dlmt, tail_dlmt)
print ( between_pattern)
compiled_pattern = re .compile ( between_pattern)
# Case 0: good case: It captures the first inner words.
result0 = re .search ( compiled_pattern, line0)
print ( "original 0 : {0}" .format ( result0.group ( 0 ) ) )
print ( "expected Head : abc_Head_def_Head" )
print ( "found Head : {0}" .format ( result0.group ( 1 ) ) )
print ( "expected Tail : Tail_ghi_Tail_jkl" )
print ( "found Tail : {0}{1}" .format ( ' ' * ( result0.regs [ 2 ] [ 0 ] ) , result0.group ( 2 ) ) )
print ( )
# Case 1: Bad case: It captures the last pair words.
result1 = re .search ( compiled_pattern, line1)
print ( "original 1 : {0}" .format ( result1.group ( 0 ) ) )
print ( "expected Head : abc_Head" )
print ( "found Head : {0}" .format ( result1.group ( 1 ) ) )
print ( "expected Tail : Tail_ghi_Head_second_Tail_opq" )
print ( "found Tail : {0}{1}" .format ( ' ' * ( result1.regs [ 2 ] [ 0 ] ) , result1.group ( 2 ) ) )
print ( testReplaceBetweenWords( ) )
aW1wb3J0IHJlCmRlZiB0ZXN0UmVwbGFjZUJldHdlZW5Xb3JkcygpOgoKICAgIGhlYWRfZGxtdD0nSGVhZCcKICAgIHRhaWxfZGxtdD0nVGFpbCcKCiAgICBsaW5lMCA9ICJhYmNfSGVhZF9kZWZfSGVhZF9pbm5lcl9pbnNpZGVfVGFpbF9naGlfVGFpbF9qa2wiCiAgICBsaW5lMSA9ICJhYmNfSGVhZF9maXJzdF9UYWlsX2doaV9IZWFkX3NlY29uZF9UYWlsX29wcSIKCiAgICBiZXR3ZWVuX3BhdHRlcm4gPSAiXigoPzooPyF7MX0pLikqezB9KS4qPyh7MX0uKikkIi5mb3JtYXQoaGVhZF9kbG10LC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