from typing import Optional
import re


def validate_and_return_regex(regex: Optional[str]) -> Optional[str]:
    """Validate a regex and return it.

    Return None in case the regex is invalid.
    """
    if regex is None:
        return None
    try:
        re.compile(regex)
        return regex
    except re.error:
        return None