4 ms·
The most difficult part of regex isn't defining the problem. I'd say that one's easy. The hardest part is figuring out your regex months or years after you've w
by zecho 13y ago
The most difficult part of regex isn't defining the problem. I'd say that one's easy. The hardest part is figuring out your regex months or years after you've written it!
- riffraff 13y agoextended 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.
- xradionut 13y agoI comment them at the time of creation and add them to my snippets file with the comment.
- maaaats 13y agoHopefully you at least wrap the regex in a function, then it's easier to reason about given the function name. Regexes randomly placed in long functions are harder to grasp.