def transform_regex_replace(source, pattern, rewrite, name=None):
    """Replace all substrings from `needle` to corresponding strings in `haystack` with source.

    Args:
        source: `Tensor` or `SparseTensor` of any shape, source strings for replacing.
        pattern: List of RE2 patterns to search in source
        rewrite: List of strings to replace with. Should have same length as `needle`.
        name: A name for the operation (optional).
    Returns:
        `Tensor` or `SparseTensor` of same shape and size as input.
    """

    with ops.name_scope(name, "TransformRegexReplace", [source]):
        source = convert_to_tensor_or_sparse_tensor(source, dtype=tf.string)
        if isinstance(source, tf.SparseTensor):
            result = tf.SparseTensor(
                indices=source.indices,
                values=ops_module.transform_regex_replace(source.values, pattern, rewrite),
                dense_shape=source.dense_shape
            )
        else:
            result = ops_module.transform_regex_replace(source, pattern, rewrite)

        return result