Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One trick I've used that saves life when designing and reviewing regex and makes them readable even for later programmers who know nothing about regex is to chunk them into smaller bits with significant names.

For example, using C, file-local, pre-processor:

#define MATCH_NAME_RE "[a-zA-Z]+"

#define MATCH_SPACES_RE "\s+"

#define CAPTURE_NUMBER_RE "(\d+\)"

And then:

   re = QRegex( MATCH_NAME_RE MATCH_SPACES_RE CAPTURE_NUMBER_RE );
It both documents what each part does, makes it easier to debug and change them later on and makes them grokable. It only breaks down when you're doing very complex matching, but most of the time RE can be broken into smaller pieces.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: