3 ms·
> There's no space for comments. For every non-trivial regex I write, I comment it heavily. I just put it together using string concatenation, like: var r =
by crazygringo 4y ago
> There's no space for comments.
For every non-trivial regex I write, I comment it heavily. I just put it together using string concatenation, like:
var r = "/[...]" + // comment goes here
"..." + // another comment
"(" +
"..." // yet another comment
"|..." // and another
and so on. Works like a charm, and I make sure to indent on the parentheses as well.
Also in the past when a regex has various parts repeated, I'll define regex "parts" as variables, and then use those.
You can make regexes as clear as you like.
- sigzero 4y agoPerl makes it easy to do as well. # Delete (most) C comments. $program =~ s { /\* # Match the opening delimiter. .*? # Match a minimal number of characters. \*/ # Match the closing delimiter. } []gsx; And I always do that to remind myself what I am doing it for.
- HybridCurve 4y agoYes, perl regex options are great for commenting and overall it's one of best things about the language. I also will paste one or more commented example lines of above my regexes so it is clear what kind of input you are expected to be processing, it always ends up being a time saver when debugging or updating code.
- deafpolygon 4y agoI learned regex on perl, and the community (at the time) was always so good about commenting their regex strings in a similar fashion.