def _elem_match_tag_pattern()

in python/de-identifier/research_pacs/de_identifier/dicom_tag_path_pattern.py [0:0]


def _elem_match_tag_pattern(elem, tag_pattern):
  """
  Return `True` if the pydicom Data Element `elem` matches the tag pattern.
  
  Args:
    elem: pydicom Data Element
    tag_pattern (str): Tag pattern
    
  """
  # Check if it matches TAG_PATH_PATTERN_KEYWORD
  if elem.keyword and re.fullmatch(TAG_PATH_PATTERN_KEYWORD, tag_pattern):
    re_pattern = re.escape(tag_pattern)
    re_pattern = re_pattern.replace('\*', '.*')
    if re.fullmatch(re_pattern, elem.keyword):
      return True

  elem_tag_hexa = _get_elem_tag_hexa(elem)

  # Check if it matches TAG_PATH_PATERN_NUMBER
  if re.fullmatch(TAG_PATH_PATERN_NUMBER, tag_pattern):
    if _tag_hexa_match_pattern(elem_tag_hexa, tag_pattern):
      return True
  
  # Check if it matches TAG_PATH_PATTERN_PRIVATE_CREATOR
  if elem.private_creator and re.fullmatch(TAG_PATH_PATTERN_PRIVATE_CREATOR, tag_pattern):
    if _tag_hexa_match_pattern(elem_tag_hexa[:4], tag_pattern[:4]) and _tag_hexa_match_pattern(elem_tag_hexa[-2:], tag_pattern[-2:]) and elem.private_creator == tag_pattern[5:-3]:
      return True
  
  # Check if it matches TAG_PATH_PATTERN_VR
  if re.fullmatch(TAG_PATH_PATTERN_VR, tag_pattern):
    if elem.VR == tag_pattern[1:-1]:
      return True
  
  return False