[英]ruby regex for php comment block
我一直在尝试在ruby中找到正则表达式以匹配php注释块:
/**
* @file
* lorum ipsum
*
* @author ME <me@localhost>
* @version 00:00 00-00-0000
*/
任何人都可以帮助我尝试搜索很多东西,即使我发现的某些正则表达式已在正则表达式测试器中工作,但是当我将其写入我的ruby文件中却没有。
这是我发现的最成功的正则表达式:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)
这是我脚本的输出
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: *
这是我要做的代码:
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
似乎/\\/\\*.*?\\*\\//m
应该可以。 同样,这实际上是c样式的注释块。
除非注释块中的每一行以星号开头很重要,否则您可能要尝试使用此正则表达式:
/\/\*(?:[^*]+|\*+(?!\/))*\*\//
编辑:这是一个更严格的版本,它将仅匹配完全按照您的示例格式设置的注释:
/^( *)\/\*\*\n(?:\1 \*(?:[^*\n]|\*(?!\/))*\n)+\1 \*\//
此版本仅与在单独的行上带有/**
和*/
的注释匹配。 可以将/**
缩进任意数量的空格 (但不能缩进其他空格字符),但是其他行必须缩进比/**
行多一个空格。
编辑2:这是另一个版本:
/^([ \t]*)\/\*\*.*?\n(?:^\1 .*?\n)+^\1 \*\//
它允许使用制表符和空格(ew)的混合形式进行缩进,但仍要求所有行都符合/**
的缩进(加上一个空格)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.