Regex Builder & Parser
Prerequisites & Dependencies
- Node.js 18+ or Python 3.10+ with regex engine knowledge
npm i redoS (for safety scoring) or Python's re module + rstr for generation
- Comfortable with character classes, quantifiers, grouping, and anchors
Execution Steps
- Clearly define the input format and extraction goal: what substrings, delimiters, or patterns must be captured
- Sketch the regex on paper first: anchors (
^, $), delimiters, optional groups, alternation (|)
- Build the regex incrementally, testing each component against sample inputs before compositing
- Use atomic groups
(?>...) or possessive quantifiers ++/*+` (if supported) to prevent backtracking exploits
- Score the regex for ReDoS risk:
npm i redoS → checkRegex(regex) should report low catastrophic backtracking risk
- Favor simpler alternatives: string methods (
split, match), String.prototype.replace, or parser combinators if the pattern exceeds ~20 characters or has nested quantifiers
- Document the final regex with inline comments
/** @type {RegExp} */ and a brief explanation of each section
// Safe regex: extract filename without extension from a path
// Breakdown: ^ anchors start, [^/]+ matches one or more non-slash chars, \. matches literal dot, $ anchors end
// Atomic group prevents backtracking on malicious inputs
const safeFilenameRegex = /^(?>[^/]+)\.([^.]+)$/;
// Test cases
const tests = [
{ input: '/path/to/document.pdf', expected: 'document', desc: 'pdf extension' },
{ input: 'archive.tar.gz', expected: 'archive', desc: 'double extension (should match first)' },
{ input: 'noext', expected: 'noext', desc: 'no dot, return basename' },
];
tests.forEach(({ input, expected, desc }) => {
const match = input.match(safeFilenameRegex);
const ok = match && match[1] === expected;
console.log(`${desc}: "${input}" → ${match ? match[1] : 'null'} ${ok ? '✅' : '❌'}`);
});
# ReDoS safety check (Node)
const { checkRegex } = require('redoS');
const regex = /^(?>[^/]+)\.([^.]+)$/;
const result = checkRegex(regex, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.');
console.log(result); // { safe: true, maxIterations: ... } or warns about backtracking
1---2name: regex-builder-parser3description: Construct ReDoS-safe regular expressions and string parser logic for complex input string extraction.4metadata:5 source: skills/regex-builder-parser/regex-builder-parser.md6---78# Regex Builder & Parser910## Prerequisites & Dependencies11- Node.js 18+ or Python 3.10+ with regex engine knowledge12- `npm i redoS` (for safety scoring) or Python's `re` module + `rstr` for generation13- Comfortable with character classes, quantifiers, grouping, and anchors1415## Execution Steps161. Clearly define the input format and extraction goal: what substrings, delimiters, or patterns must be captured172. Sketch the regex on paper first: anchors (`^`, `$`), delimiters, optional groups, alternation (`|`)183. Build the regex incrementally, testing each component against sample inputs before compositing194. Use atomic groups `(?>...)` or possessive quantifiers `++`/`*`+` (if supported) to prevent backtracking exploits205. Score the regex for ReDoS risk: `npm i redoS` → `checkRegex(regex)` should report low catastrophic backtracking risk216. Favor simpler alternatives: string methods (`split`, `match`), `String.prototype.replace`, or parser combinators if the pattern exceeds ~20 characters or has nested quantifiers227. Document the final regex with inline comments `/** @type {RegExp} */` and a brief explanation of each section2324```javascript25// Safe regex: extract filename without extension from a path26// Breakdown: ^ anchors start, [^/]+ matches one or more non-slash chars, \. matches literal dot, $ anchors end27// Atomic group prevents backtracking on malicious inputs28const safeFilenameRegex = /^(?>[^/]+)\.([^.]+)$/;2930// Test cases31const tests = [32 { input: '/path/to/document.pdf', expected: 'document', desc: 'pdf extension' },33 { input: 'archive.tar.gz', expected: 'archive', desc: 'double extension (should match first)' },34 { input: 'noext', expected: 'noext', desc: 'no dot, return basename' },35];3637tests.forEach(({ input, expected, desc }) => {38 const match = input.match(safeFilenameRegex);39 const ok = match && match[1] === expected;40 console.log(`${desc}: "${input}" → ${match ? match[1] : 'null'} ${ok ? '✅' : '❌'}`);41});42```4344```bash45# ReDoS safety check (Node)46const { checkRegex } = require('redoS');47const regex = /^(?>[^/]+)\.([^.]+)$/;48const result = checkRegex(regex, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.');49console.log(result); // { safe: true, maxIterations: ... } or warns about backtracking50```51```