I have a regex like this
(?<!(\w/))$#Cannot end with a word and slash
I would like to extract the comment from the end. While the example does not reflect this case, there could be a regex with includes regex on hashes.
\##value must be a hash
What would the regex be to extract the comment ensuring it is safe when used against regex which could contain #’s that are not comments.
Here’s a .Net flavored Regex for partly parsing .Net flavor patterns, which should get pretty close:
Luckily, in .Net each capturing group remembers all of its captures, and not just the last, so we can find all captures of the
Commentgroup in a single parse. The regex pretty much parses regular expression – but hardly fully, it just parses enough to find comments.Here’s how you use the result:
Working example: http://ideone.com/YP3yt
One last word of caution – this regex assumes the whole pattern is in
IgnorePatternWhitespacemode. When it isn’t set, all#are matched literally. Keep in mind the flag might change multiple times in a single pattern. In(?-x)#(?x)#comment, for example, regardless ofIgnorePatternWhitespace, the first#is matched literally,(?x)turns theIgnorePatternWhitespaceflag back on, and the second#is ignored.If you want a robust solution you can use a regex-language parser.
You can probably adapt the .Net source code and extract a parser: