简体   繁体   English

红宝石正则表达式的PHP注释块

[英]ruby regex for php comment block

I've been trying to find the regex in ruby to match a php comment block: 我一直在尝试在ruby中找到正则表达式以匹配php注释块:

/**
 * @file
 * lorum ipsum
 * 
 * @author  ME <me@localhost>
 * @version 00:00 00-00-0000
 */

Could anyone help I've tried searching alot and even though some regex I found has worked in a regex tester but doesn't when I write it in my ruby file. 任何人都可以帮助我尝试搜索很多东西,即使我发现的某些正则表达式已在正则表达式测试器中工作,但是当我将其写入我的ruby文件中却没有。

This is the most successful bit of regex I have found: 这是我发现的最成功的正则表达式:

 (/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)

This is the output from my script 这是我脚本的输出

file is ./test/123.rb so regex is ((^\s*#\s)+(.*?))+
i = 0
found: my first ruby comment
file is ./test/abc.php so regex is (/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)
i = 0
found: * 
i = 1
found: *

Here is the code I have to do this: 这是我要做的代码:

 56   def self.extract_comments f
 57     if @regex[File.extname(f)]
 58       puts "file is " + f + " so regex is " + @regex[File.extname(f)]
 59       cur_rgx = Regexp.new @regex[File.extname(f)]
 60       matches = IO.read( f ).scan( cur_rgx )
 61       content = ""
 62       if ! matches.empty?
 63         # content = "== " + f + " ==\n"
 64         content += f + "\n"
 65         for i in 0...f.length
 66           content += "="
 67         end
 68         content += "\n"
 69         for i in 0...matches.length
 70           puts "i = " + i.to_s
 71           puts "found: " + matches[i][2].to_s
 72           content << matches[i][2].to_s + "\n"
 73         end
 74         content << "\n"
 75       end
 76     end
 77     content || '' # return something
 78   end

It seems like /\\/\\*.*?\\*\\//m should do. 似乎/\\/\\*.*?\\*\\//m应该可以。 Also that's really a c-style comment block. 同样,这实际上是c样式的注释块。

Unless it is important that each line inside the comment block begins with an asterisk, you may want to try this regex: 除非注释块中的每一行以星号开头很重要,否则您可能要尝试使用此正则表达式:

/\/\*(?:[^*]+|\*+(?!\/))*\*\//

EDIT: And here's a stricter version, which will only match comments that are formatted exactly like your example: 编辑:这是一个更严格的版本,它将仅匹配完全按照您的示例格式设置的注释:

/^( *)\/\*\*\n(?:\1 \*(?:[^*\n]|\*(?!\/))*\n)+\1 \*\//

This version will only match a comment that has /** and */ on separate lines. 此版本仅与在单独的行上带有/***/的注释匹配。 /** can be indented by an arbitrary number of spaces (but no other white-space characters), but the other lines must be indented by exactly one more space than the /** line. 可以将/**缩进任意数量的空格 (但不能缩进其他空格字符),但是其他行必须缩进比/**行多一个空格。

EDIT 2: Here is another version: 编辑2:这是另一个版本:

/^([ \t]*)\/\*\*.*?\n(?:^\1 .*?\n)+^\1 \*\//

It allows a mixture of tabs and spaces (ew) for indentation, but still requires all lines to conform to the indentation of the /** one (plus a single space). 它允许使用制表符和空格(ew)的混合形式进行缩进,但仍要求所有行都符合/**的缩进(加上一个空格)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM