import re


def _ReplaceIndices(language, expression):
    """Replaces array access with underscored variable.

    For matrices: `variable[3,7]` becomes `variable_3_7`
    For vectors:  `variable[3]` becomes `variable_3`

    The accessor for matrices is chosen according to the language (`[]` vs. `()`)
    """
    #Matrices
    pattern = r"\[(\d+),(\d+)\]" if language == 'python' else r"\((\d+),(\d+)\)"
    replacement = r"_\1_\2"
    expression = re.sub(pattern, replacement, expression)

    # Vectors
    pattern = r"\[(\d+)\]"
    replacement = r"_\1"
    expression = re.sub(pattern, replacement, expression)

    return expression