Depurar regex patrones paso por paso
Depurar regex por testing against sample inputs, visualizing el coincidir, y understanding por qué un patrón matches o fails. Usar flags correctly.
- 1
Debugging regex: (1) Test against inputs that should match and should not match. (2) Check flags — g (global), i (case-insensitive), m (multiline), s (dotall). (3) Use a visualizer to see how the engine matches. (4) Simplify the pattern — remove parts until it works, then add back. (5) Check for common mistakes: forgetting to escape special chars, wrong quantifier, wrong anchor.
- 2 Probador de regex →
Test the pattern against multiple inputs. Start with simple cases that should match, then edge cases (empty string, special characters, Unicode). The tool shows all matches and captured groups, helping identify where the pattern diverges from expectation.
- 3 Reemplazo con regex →
Test the pattern in a replacement to see how it behaves in context. Sometimes a pattern matches correctly but the replacement reveals an issue (e.g., capturing the wrong group, off-by-one in quantifier).
Preguntas frecuentes
- Why does my regex match nothing?
- Common causes: missing global flag (g) for multiple matches, pattern is too restrictive (wrong quantifier, missing alternation), special characters not escaped (. matches any char, \. matches literal dot), case sensitivity (add i flag), anchors in wrong position. Test with a minimal input that should match.
- What is the difference between m and s flags?
- m (multiline) makes ^ and $ match at line boundaries (not just string start/end). s (dotall, single-line) makes . match newlines too. They are independent: m affects ^/$, s affects .. Use both (ms or gm) if you want . to match newlines and ^/$ to match line boundaries.