import re


def _ExprOverwrite(expr, true_variables):
  """Overwrite variables in |expr| based on |true_variables|.

  Overwrite variables in |expr| with 'True' if they occur in |true_variables|,
  'False' otherwise.

  Args:
    expr: The orginal boolean expression, like 'A and B'.
    true_variables: Collection of variable names to be considered True, e.g.
    {'A'}.

  Returns:
    A boolean expression string with pyparsing.Keyword, like 'True and False'.
  """
  # When true_variables is None, replace it with empty collection ()
  target_set = set(true_variables or ())
  items = {
      x.strip() for x in re.split(r'(?i) and | or |not |\(|\)', expr)
      if x.strip()}
  boolstr = expr
  for item in items:
    boolstr = boolstr.replace(
        item, 'True' if item in target_set else 'False')

  return boolstr