function parse_references_section
Parses a formatted references section string and extracts structured data including reference numbers, sources, and content previews using regular expressions.
/tf/active/vicechatdev/improved_convert_disclosures_to_table.py
269 - 289
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function extract_total_references 66.7% similar
-
function extract_warranty_data_improved 58.4% similar
-
function extract_warranty_data 54.7% similar
-
function format_inline_references 53.9% similar
-
class ReferenceManager_v1 53.1% similar