DEV Community

Discussion on: How do you feel about regex?

Collapse
 
ryencode profile image
Ryan Brown

When I use RegEx's I try to follow these rules (more like a guideline 🏴‍☠️)

  1. Keep the inputs shorter (don't usually regex over whole documents)
  2. Don't do too much in each regex (follow up on matches to further refine if needed)
  3. Don't let the user put in regex (unless you're building a tool for super-advanced-users)
  4. Expect non-matches and handle those
  5. Over-Test, test, test again, automate the tests, and ensure any related issue/bug reported is added as a case to the tests.

My context is usually used in file name parsing as my users are supposed to name their documents in a meaningful, structured way for ingestion into other systems. The exception report (files not match-able) typically runs longer than the source code ;) Turns out people just can't spell common words consistently when it matters. (I'm no exception)