4 ms·
extended regexes with comments (aka /x) should be the default and enforced behaviour in all newly designed languages. Sadly, the only one that does is perl6.
by riffraff 13y ago
extended regexes with comments (aka /x) should be the default and enforced behaviour in all newly designed languages.
Sadly, the only one that does is perl6.
- pierrebai 13y agoOne 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.
- deckiedan 13y agoPython has them: a = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X)
- zyxley 13y agoCoffeescript has similar functionality. From the Coffeescript docs: OPERATOR = /// ^ ( ?: [-=]> # function | [-+*/%<>&|^!?=]= # compound assign / compare | >>>=? # zero-fill right shift | ([-+:])\1 # doubles | ([&|<>])\2=? # logic / shift | \?\. # soak access | \.{2,3} # range or splat ) /// When turned into Javascript it becomes: var OPERATOR; OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/;
- riffraff 13y agoyes, any decent PL nowadays has it, my point was that it should be the default.