| import re |
| import json |
| from pathlib import Path |
|
|
| def parse_regex_file(file_path: str): |
| """ |
| Parser robusto para o formato: |
| buscar por / bus por |
| PATTERN |
| substituir por / sub por |
| REPLACEMENT |
| """ |
| content = Path(file_path).read_text(encoding="utf-8") |
| |
| |
| blocks = re.split(r'(?i)bus(?:car)?\spor:?\s*', content) |
| |
| regex_pairs = [] |
| |
| for block in blocks: |
| if not block.strip(): |
| continue |
| |
| |
| parts = re.split(r'(?i)sub(?:stituir)?\spor:?\s*', block) |
| |
| if len(parts) >= 2: |
| pattern = parts[0].strip() |
| replacement = parts[1].strip() |
| |
| |
| |
| if pattern and replacement: |
| regex_pairs.append({ |
| "search": pattern, |
| "replace": replacement |
| }) |
| |
| return regex_pairs |
|
|
| if __name__ == "__main__": |
| pairs = parse_regex_file("regex_anuncios.txt") |
| with open("regex_config.json", "w", encoding="utf-8") as f: |
| json.dump(pairs, f, indent=4, ensure_ascii=False) |
| print(f"Sucesso: {len(pairs)} pares de Regex extraídos para regex_config.json") |
|
|