🔍 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, analysis, or export. Common use cases include processing research papers, documentation with citations, or any text with numbered references that need to be cataloged or analyzed programmatically.

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 references marked with **[number]** followed by source information and optionally a '*Content preview*:' line with preview text. The string can contain multiple references separated by newlines or the next reference marker.

Return Value

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

Dependencies

  • re

Required Imports

import re

Usage Example

import re

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

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

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

Best Practices

  • Ensure the input string follows the expected format with **[number]** markers for reference numbers
  • The function expects markdown-style bold formatting (**) around reference numbers
  • Content previews are optional; the function handles references without previews gracefully
  • The regex pattern uses DOTALL flag, so newlines within source or preview text are handled
  • Consider validating the input string format before calling this function to avoid empty results
  • The function strips whitespace from source and preview text automatically
  • Reference numbers are returned as strings, not integers; convert if numeric operations are needed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function extract_total_references 66.7% 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 extract_warranty_data_improved 58.4% 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 extract_warranty_data 54.7% 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
  • function format_inline_references 53.9% similar

    Formats inline citation references (e.g., [1], [2]) in a Word document paragraph by applying italic styling to them while preserving the rest of the text.

    From: /tf/active/vicechatdev/enhanced_word_converter_fixed.py
  • class ReferenceManager_v1 53.1% similar

    Manages document references for inline citation and bibliography generation, tracking documents and generating formatted citations and bibliographies.

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