s/^ # Beginning of string [^|]+ # Everything up to the next bar \| # The bar ( # Capture to $1 [^|]+ # Everything up to the next bar ) # End capture \| # The bar [^|]+ # Everything up to the next bar \| # The bar (?: # Non-capturing parens internal # the string "internal" | # or partner # the string "partner" )$ # End of string /$1/x # Substitute $1 for string