import re


def booleannet2bnet(rules):
    """Converts BooleanNet rules to BNet format.
    e.g., an input of
    "A*=B or C and not D"
    returns
    A,  B | C & !D

    Also replaces ~ with !

    Parameters
    ----------
    rules : str
        BooleanNet formatted rules.

    Returns
    -------
    str
        BNET formatted rules.

    """
    s = re.sub("\s*\*\s*=\s*",",\t",rules) # replace "=" with ",\t"
    s = re.sub("\s+not\s+"," !",s, flags=re.IGNORECASE) # not -> !
    s = re.sub("\(\s*not\s+","(!",s, flags=re.IGNORECASE) # not -> ! (with parens)
    s = re.sub("\s*~\s*"," !",s, flags=re.IGNORECASE) # ~ -> !
    s = re.sub("\s+and\s+"," & ",s, flags=re.IGNORECASE) # and -> &
    s = re.sub("\s+or\s+"," | ",s, flags=re.IGNORECASE) # or -> |
    s = re.sub("False","0",s, flags=re.IGNORECASE) # False -> 0 (ignore case)
    s = re.sub("True","1",s, flags=re.IGNORECASE) # True -> 1 (ignore case)
    return s