regex-config-pattern
Generate regex patterns for config_manager.py save operations. Use when adding config save logic, creating regex for BobConfig.py updates, or troubleshooting config regex patterns.
When & Why to Use This Skill
This Claude skill provides a specialized toolkit for generating precise Python regex patterns tailored for configuration management. It streamlines the development of `config_manager.py` by providing robust, type-aware regex strings for updating `BobConfig.py` files. The skill ensures that configuration updates handle various data types—including booleans, integers, floats, strings, and lists—while preserving code formatting and inline comments, significantly reducing the risk of syntax errors during automated file modifications.
Use Cases
- Scenario 1: Developing automated save logic for new configuration parameters in a Python project to ensure persistent settings.
- Scenario 2: Creating non-greedy regex patterns to safely update specific string or list values without accidentally matching or overwriting adjacent code.
- Scenario 3: Troubleshooting and debugging failing regex matches in configuration update scripts by comparing against standardized, type-specific pattern templates.
- Scenario 4: Implementing complex configuration updates that require preserving existing code comments and handling multi-line list structures using DOTALL flags.
| name | regex-config-pattern |
|---|---|
| description | Generate regex patterns for config_manager.py save operations. Use when adding config save logic, creating regex for BobConfig.py updates, or troubleshooting config regex patterns. |
| allowed-tools | Read |
Regex Configuration Patterns
Generates the correct regex patterns for config_manager.py save operations that update BobConfig.py.
When to Use
- Adding save logic to config_manager.py
- Creating regex patterns for new config parameters
- Troubleshooting regex matching issues
- Understanding the config save pattern
Pattern Structure
All config saves follow this pattern:
content = re.sub(
r'PATTERN_TO_MATCH',
f'REPLACEMENT_STRING',
content
)
changes_made.append(f"PARAM_NAME = {value}")
Type-Specific Patterns
Boolean Parameters
BobConfig.py:
FEATURE_ENABLED: bool = True
Regex Pattern:
if 'feature_enabled' in section:
content = re.sub(
r'FEATURE_ENABLED:\s*bool\s*=\s*(True|False)',
f'FEATURE_ENABLED: bool = {section["feature_enabled"]}',
content
)
changes_made.append(f"FEATURE_ENABLED = {section['feature_enabled']}")
Pattern Breakdown:
FEATURE_ENABLED:- Exact parameter name + colon\s*- Zero or more whitespacebool- Type annotation\s*=\s*- Equals sign with optional whitespace(True|False)- Either True or False (capture group)
Integer Parameters
BobConfig.py:
MAX_RETRIES: int = 3
Regex Pattern:
if 'max_retries' in section:
content = re.sub(
r'MAX_RETRIES:\s*int\s*=\s*\d+',
f'MAX_RETRIES: int = {section["max_retries"]}',
content
)
changes_made.append(f"MAX_RETRIES = {section['max_retries']}")
Pattern Breakdown:
\d+- One or more digits (matches any integer)
Float Parameters
BobConfig.py:
THRESHOLD: float = 0.5
PERCENTAGE: float = 10.0
Regex Pattern:
if 'threshold' in section:
content = re.sub(
r'THRESHOLD:\s*float\s*=\s*[\d.]+',
f'THRESHOLD: float = {float(section["threshold"])}',
content
)
changes_made.append(f"THRESHOLD = {section['threshold']}")
Pattern Breakdown:
[\d.]+- One or more digits or decimal points- Note:
float()conversion in f-string ensures proper formatting
Important: Always cast to float in f-string!
# ✓ Correct
f'THRESHOLD: float = {float(section["threshold"])}'
# ✗ Wrong - may produce "0.5" vs "0.50"
f'THRESHOLD: float = {section["threshold"]}'
String Parameters
BobConfig.py:
MODEL_NAME: str = "default"
API_URL: str = "http://localhost:5000"
Regex Pattern:
if 'model_name' in section:
content = re.sub(
r'MODEL_NAME:\s*str\s*=\s*"[^"]*"',
f'MODEL_NAME: str = "{section["model_name"]}"',
content
)
changes_made.append(f"MODEL_NAME = {section['model_name']}")
Pattern Breakdown:
"[^"]*"- Matches string in double quotes[^"]- Any character except double quote*- Zero or more characters
For single quotes:
r"MODEL_NAME:\s*str\s*=\s*'[^']*'"
f"MODEL_NAME: str = '{section[\"model_name\"]}'"
Optional Parameters
BobConfig.py:
API_KEY: Optional[str] = None
Regex Pattern:
if 'api_key' in section:
content = re.sub(
r'API_KEY:\s*Optional\[str\]\s*=\s*(?:"[^"]*"|None)',
f'API_KEY: Optional[str] = "{section["api_key"]}" if section["api_key"] else "None"',
content
)
changes_made.append(f"API_KEY = {section['api_key']}")
Pattern Breakdown:
Optional\[str\]- Type annotation (brackets escaped)(?:"[^"]*"|None)- Non-capturing group: either string or None"[^"]*"- String in quotes|- ORNone- Literal None
List Parameters
BobConfig.py:
ALLOWED_MODELS: List[str] = ["model1", "model2"]
Regex Pattern (Complex):
if 'allowed_models' in section:
models_str = str(section["allowed_models"]) # Convert list to string
content = re.sub(
r'ALLOWED_MODELS:\s*List\[str\]\s*=\s*\[.*?\]',
f'ALLOWED_MODELS: List[str] = {models_str}',
content
)
changes_made.append(f"ALLOWED_MODELS = {len(section['allowed_models'])} items")
Pattern Breakdown:
\[.*?\]- Matches list brackets with any content.*?- Non-greedy match (minimum characters)
Common Pattern Variations
With Comments
BobConfig.py:
MAX_FPS: int = 30 # Maximum frames per second
Regex (preserves comment):
r'MAX_FPS:\s*int\s*=\s*\d+(\s*#.*)?'
Pattern Breakdown:
(\s*#.*)?- Optional group for comment\s*#- Whitespace + hash.*- Rest of comment?- Group is optional
Multiline Parameters
BobConfig.py:
LONG_LIST: List[str] = [
"item1",
"item2"
]
Use re.DOTALL flag:
content = re.sub(
r'LONG_LIST:\s*List\[str\]\s*=\s*\[.*?\]',
f'LONG_LIST: List[str] = {value}',
content,
flags=re.DOTALL
)
Complete Save Method Template
def save_<section>_settings(self, settings: Dict) -> Dict:
"""Save <section> settings to BobConfig.py"""
try:
config_path = Path(__file__).parent.parent / 'BobConfig.py'
if not config_path.exists():
return {'success': False, 'message': f'Config file not found: {config_path}'}
# Read current config
with open(config_path, 'r', encoding='utf-8') as f:
content = f.read()
changes_made = []
# Boolean parameter
if 'bool_param' in settings['section']:
content = re.sub(
r'BOOL_PARAM:\s*bool\s*=\s*(True|False)',
f'BOOL_PARAM: bool = {settings["section"]["bool_param"]}',
content
)
changes_made.append(f"BOOL_PARAM = {settings['section']['bool_param']}")
# Integer parameter
if 'int_param' in settings['section']:
content = re.sub(
r'INT_PARAM:\s*int\s*=\s*\d+',
f'INT_PARAM: int = {settings["section"]["int_param"]}',
content
)
changes_made.append(f"INT_PARAM = {settings['section']['int_param']}")
# Float parameter
if 'float_param' in settings['section']:
content = re.sub(
r'FLOAT_PARAM:\s*float\s*=\s*[\d.]+',
f'FLOAT_PARAM: float = {float(settings["section"]["float_param"])}',
content
)
changes_made.append(f"FLOAT_PARAM = {settings['section']['float_param']}")
# String parameter
if 'str_param' in settings['section']:
content = re.sub(
r'STR_PARAM:\s*str\s*=\s*"[^"]*"',
f'STR_PARAM: str = "{settings["section"]["str_param"]}"',
content
)
changes_made.append(f"STR_PARAM = {settings['section']['str_param']}")
if not changes_made:
return {'success': False, 'message': 'No valid settings to update'}
# Write updated config
with open(config_path, 'w', encoding='utf-8') as f:
f.write(content)
logger.info(f"Updated settings: {', '.join(changes_made)}")
return {
'success': True,
'message': f'Settings saved. Updated: {", ".join(changes_made)}'
}
except Exception as e:
logger.error(f"Error saving settings: {e}", exc_info=True)
return {'success': False, 'message': f'Error: {str(e)}'}
Testing Regex Patterns
Python REPL Test:
import re
# Test content
content = "THRESHOLD: float = 0.5 # Detection threshold"
# Test pattern
pattern = r'THRESHOLD:\s*float\s*=\s*[\d.]+'
replacement = f'THRESHOLD: float = {float(10.0)}'
result = re.sub(pattern, replacement, content)
print(result)
# Output: THRESHOLD: float = 10.0 # Detection threshold
Online Regex Tester:
- https://regex101.com/ (Python flavor)
- Paste BobConfig.py line
- Test pattern
- Verify match and replacement
Common Mistakes
1. Wrong Pattern for Float
# ✗ Wrong - only matches integers
r'THRESHOLD:\s*float\s*=\s*\d+'
# ✓ Correct - matches floats with decimals
r'THRESHOLD:\s*float\s*=\s*[\d.]+'
2. Missing Float Conversion
# ✗ Wrong - may lose precision
f'THRESHOLD: float = {value}'
# ✓ Correct - explicit float conversion
f'THRESHOLD: float = {float(value)}'
3. Greedy vs Non-Greedy
# ✗ Wrong - matches too much (greedy)
r'PARAM:\s*str\s*=\s*".*"' # Matches from first to LAST quote
# ✓ Correct - matches minimal (non-greedy)
r'PARAM:\s*str\s*=\s*"[^"]*"' # Matches one string
4. Not Escaping Special Characters
# ✗ Wrong - brackets are regex special chars
r'List[str]'
# ✓ Correct - escape brackets
r'List\[str\]'
5. Missing changes_made Entry
# ✗ Wrong - silent success, no tracking
content = re.sub(pattern, replacement, content)
# ✓ Correct - track changes
content = re.sub(pattern, replacement, content)
changes_made.append(f"PARAM = {value}")
Debugging Regex Issues
Pattern doesn't match:
- Copy actual line from BobConfig.py
- Test in regex101.com with Python flavor
- Check for extra whitespace or comments
- Verify type annotation exact spelling
Match but wrong replacement:
- Check capture groups in pattern
- Verify f-string uses correct variable
- Test with print before writing file
Multiple matches:
- Make pattern more specific (add context)
- Use parameter name prefix (e.g.,
VISION_THRESHOLD) - Check for duplicates in BobConfig.py
Quick Reference
| Type | Pattern | Example Match |
|---|---|---|
| bool | (True|False) |
True, False |
| int | \d+ |
42, 100 |
| float | [\d.]+ |
0.5, 10.0 |
| str | "[^"]*" |
"value" |
| Optional[str] | (?:"[^"]*"|None) |
"value" or None |
| List | \[.*?\] |
["a", "b"] |
Pro Tips
- Test patterns first - Use regex101.com before coding
- Copy exact lines - Use actual BobConfig.py content for testing
- Be specific - Include parameter name to avoid false matches
- Track changes - Always append to changes_made list
- Handle errors - Wrap in try/except
- Log changes - Use logger.info for change tracking
- Validate input - Check types before regex