def filter_field_instances(field_instances, filter_subcode, filter_value,<EOL>filter_mode='<STR_LIT:e>'):
matched = []<EOL>if filter_mode == '<STR_LIT:e>':<EOL><INDENT>to_match = (filter_subcode, filter_value)<EOL>for instance in field_instances:<EOL><INDENT>if to_match in instance[<NUM_LIT:0>]:<EOL><INDENT>matched.append(instance)<EOL><DEDENT><DEDENT><DEDENT>elif filter_mode == '<STR_LIT:s>':<EOL><INDENT>for instance in field_instances:<EOL><INDENT>for subfield in instance[<NUM_LIT:0>]:<EOL><INDENT>if subfield[<NUM_LIT:0>] == filter_subcode andsubfield[<NUM_LIT:1>].find(filter_value) > -<NUM_LIT:1>:<EOL><INDENT>matched.append(instance)<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif filter_mode == '<STR_LIT:r>':<EOL><INDENT>reg_exp = re.compile(filter_value)<EOL>for instance in field_instances:<EOL><INDENT>for subfield in instance[<NUM_LIT:0>]:<EOL><INDENT>if subfield[<NUM_LIT:0>] == filter_subcode andreg_exp.match(subfield[<NUM_LIT:1>]) is not None:<EOL><INDENT>matched.append(instance)<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return matched<EOL>
Filter the given field.

    Filters given field and returns only that field instances that contain
    filter_subcode with given filter_value. As an input for search function
    accepts output from record_get_field_instances function. Function can be
    run in three modes:

    - 'e' - looking for exact match in subfield value
    - 's' - looking for substring in subfield value
    - 'r' - looking for regular expression in subfield value

    Example:

    record_filter_field(record_get_field_instances(rec, '999', '%', '%'),
                        'y', '2001')

    In this case filter_subcode is 'y' and filter_value is '2001'.

    :param field_instances: output from record_get_field_instances
    :param filter_subcode: name of the subfield
    :type filter_subcode: string
    :param filter_value: value of the subfield
    :type filter_value: string
    :param filter_mode: 'e','s' or 'r'