Regular Expressions Best Practices

Regular Expressions Best Practices

Lays out a developer's rules for readable, safe regular expressions: when to avoid them, named groups, anchoring, ReDoS defences and testing.

Regular Expressions taste of a developer who wants regexes people can still read

  • Reach for a real parser for HTML, JSON, CSV, URLs and email headers; a regex is the wrong tool there. Confidence: 0.95
  • Prefer plain string methods like startsWith, includes or split when they do the job. Confidence: 0.85
  • Anchor patterns with ^ and $ (or \A and \z) when validating a whole value. Confidence: 0.90
  • Use named capture groups instead of counting numbered groups. Confidence: 0.85
  • Use non-capturing groups (?:...) when you only need grouping, not the captured value. Confidence: 0.75
  • Write long patterns in verbose mode with comments, such as re.VERBOSE in Python or the x flag where supported. Confidence: 0.80
  • Prefer specific character classes like [^"]* over a lazy .*? to make intent clear and matching faster. Confidence: 0.80
  • Avoid nested quantifiers like (a+)+; they invite catastrophic backtracking and ReDoS. Confidence: 0.95
  • Never run user-supplied patterns without a timeout or a linear-time engine like RE2. Confidence: 0.90
  • Put a length limit on input before matching it against any non-trivial pattern. Confidence: 0.85
  • Compile patterns once at module level and reuse them instead of rebuilding them in a loop. Confidence: 0.75
  • Escape dynamic text with re.escape or an equivalent helper before putting it into a pattern. Confidence: 0.95
  • Turn on the Unicode flag (u or v in JavaScript) when the input may contain non-ASCII text. Confidence: 0.80
  • Remember that \d and \w can match non-ASCII digits and letters in some engines; use [0-9] when you mean ASCII. Confidence: 0.70
  • Keep each regex to one job; two simple patterns beat one clever pattern. Confidence: 0.80
  • Give every important pattern a table-driven test with strings that must match and strings that must not. Confidence: 0.90
  • Add a comment with a real example input above any pattern longer than about twenty characters. Confidence: 0.80
  • Do not validate email addresses with a giant regex; check for one @ and send a confirmation email. Confidence: 0.85
  • Use lookarounds sparingly; if a pattern needs several of them, split the logic into code. Confidence: 0.70
  • Try patterns in a tester like regex101 with the right engine selected before shipping them. Confidence: 0.70

Public domain (CC0 1.0) · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 13,387 prompts

More by @OpenSourceBuffGuy

See all
Nine expression portrait collage with film borders prompt image

Nine expression portrait collage with film borders

Edits an uploaded photo into a 9-frame photobooth portrait collage with a different playful expression in each frame, streetwear outfit and soft studio light.

image-prompt, gpt-image, chatgpt, reference-image, face-reference, photo-editing, collage, multi-panel, portrait, candid, close-up, studio-lighting, soft-lighting, white-background, fashion, editorial, vertical-format, film-grain, woman, photorealistic, muted-colors, fashion-shoot, long-form, for-photographers, for-content-creators, hairstyles

Man leaning on pillar under violet-lit bridge prompt image

Man leaning on pillar under violet-lit bridge

Draws a cinematic night street portrait of a young man leaning on a pillar, with a violet-lit pedestrian bridge, traffic light streaks and a charcoal and midnight-blue palette.

image-prompt, portrait, man, night-scene, cityscape, cinematic, photorealistic, film-grain, candid, moody, dramatic-lighting, medium-shot, street-photography, motion-blur, muted-colors, neon, korea, gpt-image, gemini, for-photographers, reference-image, long-form

Window Photo Continuing Into Ink Sketch Paper prompt image

Window Photo Continuing Into Ink Sketch Paper

Turns an uploaded photo into a split canvas whose top half stays photographic and whose bottom half continues the scene as torn paper and black line doodles with a short handwritten caption.

image-prompt, gpt-image, reference-image, photo-editing, collage, papercraft, line-art, doodle, split-screen, vertical-format, negative-space, editorial, natural-light, minimalist, text-rendering, before-and-after, surreal, long-form, negative-prompt, for-artists

Reviews

Loading reviews…

Discussion

Loading comments…