import re


def replace_string( text, old_string, new_string ):
    """
    Check that `old_string` is in `text`, and replace it by `new_string`
    (`old_string` and `new_string` use regex syntax)
    """
    # Check that the target line is present
    if re.findall(old_string, text) == []:
        raise RuntimeError('Did not find expected string: %s' %old_string)
    # Return the modified text
    return re.sub( old_string, new_string, text )