replace match logic