import re


def messagestat_formatting(messageBody):
    """
    Formats the message body for messagestat functions.
    This mostly involves replacing spaces with underscores
    and doubling up on quotations so that SQL server
    ignores them
    """
    messageBody = re.sub(' ', '_', messageBody)
    messageBody = re.sub("'", "''", messageBody)
    if messageBody.startswith('"') and messageBody.endswith('"'):
        messageBody = messageBody[1:]
        messageBody = messageBody[:-1]
    return(messageBody)