proper logic on regex matching