我一直在尝试在 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