🔍 Code Extractor

function parse_references_section

Maturity: 45

Parses a formatted references section string and extracts structured data including reference numbers, sources, and content previews using regular expressions.

File:
/tf/active/vicechatdev/improved_convert_disclosures_to_table.py
Lines:
269 - 289
Complexity:
moderate

Purpose

This function is designed to parse a text block containing academic or document references formatted with markdown-style bold reference numbers (e.g., **[1]**), followed by source information and optional content previews. It extracts these components into a structured list of dictionaries for easier processing, storage, or display. Common use cases include processing bibliography sections, extracting citations from documents, or preparing reference data for export to CSV or database formats.

Source Code

def parse_references_section(references_section):
    """Parse the references section into structured data."""
    ref_data = []
    
    # Pattern to match reference entries like **[1]** Source
    ref_pattern = r'\*\*\[(\d+)\]\*\*\s*(.+?)(?=\n\s*\*Content preview\*:\s*(.+?)(?=\n\n|\*\*\[|\Z))'
    
    matches = re.findall(ref_pattern, references_section, re.DOTALL)
    
    for match in matches:
        ref_num = match[0]
        source = match[1].strip()
        preview = match[2].strip() if len(match) > 2 else ""
        
        ref_data.append({
            'Reference_Number': ref_num,
            'Source': source,
            'Content_Preview': preview
        })
    
    return ref_data

Parameters

Name Type Default Kind
references_section - - positional_or_keyword

Parameter Details

references_section: A string containing the references section to parse. Expected format includes reference entries marked with **[number]** followed by source information and optionally a '*Content preview*:' line with preview text. The string may contain multiple reference entries separated by newlines or other reference markers.

Return Value

Returns a list of dictionaries, where each dictionary represents a parsed reference entry with three keys: 'Reference_Number' (string containing the numeric reference identifier), 'Source' (string with the source/citation text), and 'Content_Preview' (string with the preview text if present, otherwise empty string). Returns an empty list if no matches are found.

Dependencies

  • re

Required Imports

import re

Usage Example

import re

references_text = '''
**[1]** Smith, J. (2020). Example Article. Journal of Examples.
*Content preview*: This article discusses various examples.

**[2]** Doe, J. (2021). Another Reference. Academic Press.
*Content preview*: A comprehensive study on references.
'''

result = parse_references_section(references_text)
print(result)
# Output: [
#   {'Reference_Number': '1', 'Source': 'Smith, J. (2020). Example Article. Journal of Examples.', 'Content_Preview': 'This article discusses various examples.'},
#   {'Reference_Number': '2', 'Source': 'Doe, J. (2021). Another Reference. Academic Press.', 'Content_Preview': 'A comprehensive study on references.'}
# ]

Best Practices

  • Ensure the input string follows the expected format with **[number]** markers for reference numbers
  • The regex pattern expects specific formatting; variations in markdown syntax may not be captured
  • Handle empty or None input by adding validation before calling this function
  • The function uses re.DOTALL flag to match across newlines, so multi-line content previews are supported
  • Consider adding error handling for malformed reference sections
  • The regex may not capture references without content previews correctly if the format varies
  • Test with various reference formats to ensure the regex pattern matches your specific use case

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function extract_total_references 66.4% similar

    Extracts the total count of references from markdown-formatted content by first checking for a header line with the total, then falling back to manually counting reference entries.

    From: /tf/active/vicechatdev/enhanced_word_converter_fixed.py
  • function process_markdown_content 59.0% similar

    Parses markdown-formatted text content and converts it into a structured list of content elements with type annotations and formatting metadata suitable for document export.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function extract_warranty_data_improved 58.3% similar

    Parses markdown-formatted warranty documentation to extract structured warranty data including IDs, titles, sections, disclosure text, and reference citations.

    From: /tf/active/vicechatdev/improved_convert_disclosures_to_table.py
  • function process_markdown_content_v1 57.1% similar

    Parses markdown-formatted text content and converts it into a structured list of document elements (headers, paragraphs, lists, tables, code blocks) with their types and formatting preserved in original order.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function extract_warranty_data 55.1% similar

    Parses markdown-formatted warranty documentation to extract structured warranty information including IDs, titles, sections, source document counts, warranty text, and disclosure content.

    From: /tf/active/vicechatdev/convert_disclosures_to_table.py
← Back to Browse