Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Are there any common practises for commenting the regular expressions: inline comments referring different part of RegEx or general comment for all expression?

share|improve this question
2  
There are but you need to be more specific. For example Bash supports inline comments and Python offers verbose regular expressions. –  faif Dec 5 '12 at 12:19
2  
My rule of thumb for regular expressions is: if you need to comment the regular expression, it's too complicated. –  zzzzBov Dec 5 '12 at 15:43
add comment

3 Answers

up vote 6 down vote accepted

In my view, a good practice is to concisely state in comments what the general idea of the regular expression is. This saves other developers (or sometimes yourself) the hassle of copy-pasting the regex in a parser like RegExr, only to understand what it does.

share|improve this answer
    
RegExr will happen anyway, unless the dev is a regex savant. But I agree with providing a general description; that's what I do with my regexes. –  Robert Harvey Dec 5 '12 at 16:22
1  
+1: Anything more detailed will end up being a crash course in regex as a comment. –  Matt Dec 5 '12 at 19:11
    
This answer and the @zzzzBov comments make sense. –  m0nhawk Dec 8 '12 at 12:44
add comment

I guess it really depends on how you're putting the regex together. Generally speaking I think it would be a bad idea to put comments within the actual regex string itself (not possible in most scenarios, as far as I know). If you really need to comment specific portions of a regular expression (are you trying to teach someone?), then break each chunk into separate strings on their own lines, and comment each line using the normal commenting process for your programming language. Otherwise, pleinolijf's answer is pretty good.

example:

string myregex = "\s" // Match any whitespace once
+ "\n"  // Match one newline character
+ "[a-zA-Z]";  // Match any letter
share|improve this answer
add comment

Typically, I will write a regex and not explain the individual pieces of the regex, but rather what it's purpose is. That is that what and why. This is a bit like asking "What should my comments look like?" to which one would say "Don't write what the code is doing, write why the code is doing what it does"

// Strip the leading "?" and remove the query parameters "offset=<integer>" & "count=<integer> so we have a pattern of the request"          
var search = location.search.substring(1).replace(/offset=[0-9]+?&/g, "").replace(/count=[0-9]+?&/g, "");

Unless you are trying to teach someone about regexes via comments in code, I don't think explaining what each individual piece will do. When working with other programmers, you can safely assume that one would know something as global regular expressions.

share|improve this answer
2  
you would be surprised... –  Matt Dec 5 '12 at 18:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.